<!--网络--><uses-permission android:name="android.permission.INTERNET" /><!-- 读写文件 --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
public static void saveToSystemGallery(Context context, Bitmap bmp) {// 首先保存图片File appDir = new ExternalStorageDirectory(), "vgmap");if (!ists()) {appDir.mkdir();}String fileName = System.currentTimeMillis() + ".jpg";File file = new File(appDir, fileName);try {FileOutputStream fos = new FileOutputStream(file);bmppress(Bitmap.CompressFormat.JPEG, 100, fos);fos.flush();fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}// 其次把文件插入到系统图库try {MediaStore.Images.Media.ContentResolver(),AbsolutePath(), fileName, null);} catch (FileNotFoundException e) {e.printStackTrace();}// 最后通知图库更新context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.AbsolutePath())));}
public class DonwloadSaveImg {private static Context context;private static String filePath;private static Bitmap mBitmap;private static String mSaveMessage = "失败";private final static String TAG = "PictureActivity";private static ProgressDialog mSaveDialog = null;public static void donwloadImg(Context contexts, String filePaths) {context = contexts;filePath = filePaths;mSaveDialog = ProgressDialog.show(context, "保存图片", "图片正在保存中,请稍等...", true);new Thread(saveFileRunnable).start();}private static Runnable saveFileRunnable = new Runnable() {@Overridepublic void run() {try {if (!TextUtils.isEmpty(filePath)) { //网络图片// 对资源链接URL url = new URL(filePath);//打开输入流InputStream inputStream = url.openStream();//对网上资源进行下载转换位图图片mBitmap = BitmapFactory.decodeStream(inputStream);inputStream.close();}saveFile(mBitmap);mSaveMessage = "图片保存成功!";} catch (IOException e) {mSaveMessage = "图片保存失败!";e.printStackTrace();} catch (Exception e) {e.printStackTrace();}messageHandler.sendMessage(messageHandler.obtainMessage());}};private static Handler messageHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {mSaveDialog.dismiss();Log.d(TAG, mSaveMessage);Toast.makeText(context, mSaveMessage, Toast.LENGTH_SHORT).show();}};/*** 保存图片* @param bm* @throws IOException*/public static void saveFile(Bitmap bm ) throws IOException {File dirFile = new ExternalStorageDirectory().getPath());if (!ists()) {dirFile.mkdir();}String fileName = UUID.randomUUID().toString() + ".jpg";File myCaptureFile = new ExternalStorageDirectory().getPath() + "/DCIM/Camera/" + fileName);BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));bmpress(Bitmap.CompressFormat.JPEG, 80, bos);bos.flush();bos.close();//把图片保存后声明这个广播事件通知系统相册有新图片到来Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);Uri uri = Uri.fromFile(myCaptureFile);intent.setData(uri);context.sendBroadcast(intent);}
}
public class DownloadSavePictureActivity extends AppCompatActivity {private Button btn_pic;private Bitmap bitmap;private String Path="你的网络图片路径";@Overrideprotected void onCreate(Bundle savedInstanceState) {Create(savedInstanceState);setContentView(R.layout.activity_download_save_picture);btn_pic=findViewById(R.id.btn_pic);btn_pic.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {ImgDonwloads.donwloadImg(DownloadSavePictureActivity.this,Path);//iPath}});}
}
本文发布于:2024-01-31 05:02:02,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170664853825731.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |