Android 保存图片到本地(亲测有效)

可子猫
发布于 2022-3-1 11:10
浏览
0收藏
/**
     * 将Bitmap图片保存到本地相册
     */
    public static void savePhotoToGallery(final Context context, final Bitmap bitmap) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            AndPermission.with((Activity) context)
                    .requestCode(200)
                    .permission(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .start();
        }


        if (bitmap == null) {
            ToastUtil.showCenterToast(context, "未获取到图片");
            return;
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                // 其次把文件插入到系统图库
                try {
                    MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap,
                            fileName, "测试 图集"); // 名字和描述没用,系统会自动更改
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtil.showCenterToast(context, "图片保存至相册");
                        }
                    });
                } catch (Exception e) {
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtil.showCenterToast(context, "图片保存失败");
                        }
                    });
                    LogUtils.e("图片保存异常:", e);
                }
            }
        }).start();
    }

/**
     * 将图片保存到本地相册
     *
     */
    public static void savePhotoToGallery(final Context context, final String imgUrl) {
        if (TextUtils.isEmpty(imgUrl)) {
            ToastUtil.showCenterToast(context,"未获取到图片");
            return;
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                String fileName = "test_" + System.currentTimeMillis() + ".jpg";
                String sdCardDir = SDCardUtils.getDiskDir() + "DCIM/";
                File appDir = new File(sdCardDir, "text");
                if (!appDir.exists()) {
                    appDir.mkdir();
                }
                File f = new File(appDir, fileName);

                try {
                    // 保存图片
                    URL url = new URL(imgUrl);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    con.setRequestMethod("GET");
                    con.setConnectTimeout(1000 * 6);
                    if (con.getResponseCode() == 200) {
                        InputStream inputStream = con.getInputStream();
                        byte[] b = FileUtils.getBytes(inputStream);
                        FileOutputStream fileOutputStream = new FileOutputStream(f);
                        fileOutputStream.write(b);
                        fileOutputStream.close();
                    } else {
                        ToastUtil.showCenterToast(context,"图片保存失败");
                        return;
                    }

                    //把文件插入到系统图库
                      MediaStore.Images.Media.insertImage(context.getContentResolver(),
                              f.getAbsolutePath(), fileName, null);

                    // 通知图库更新
                    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(f.getPath()))));

                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtil.showCenterToast(context,"图片保存至相册");
                        }
                    });
                } catch (Exception e) {
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtil.showCenterToast(context,"图片保存失败");
                        }
                    });
                    LogUtils.e("图片保存异常:", e);
                }
            }
        }).start();
    }

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.

其中的getBytes方法如下:

   /**
     * 将InputStream,转换为字节
     */
    public static byte[] getBytes(InputStream inputStream) throws Exception {
        byte[] b = new byte[1024];
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int len = -1;
        while ((len = inputStream.read(b)) != -1) {
            byteArrayOutputStream.write(b, 0, len);
        }
        byteArrayOutputStream.close();
        inputStream.close();
        return byteArrayOutputStream.toByteArray();
    }

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

分类
标签
1
收藏
回复
举报
1
回复
    相关推荐