保存图片到相册/图库___Android基础篇

文 | Promise Sun


一、描述

APP需要实现用户手动将图片保存到手机相册/图库
注:Android11 测试有效

二、实现

直接调用下面的方法即可 

  /*
     * 将图片 bitmap保存到图库
     */
    public static void saveBitmap(Context activity,Bitmap bitmap) {
        //因为xml用的是背景,所以这里也是获得背景
//获取参数Bitmap方式一: Bitmap bitmap=((BitmapDrawable)(imageView.getBackground())).getBitmap();
//获取参数Bitmap方式二: Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

        //t设置图片名称,要保存png,这里后缀就是png,要保存jpg,后缀就用jpg
        String imageName = System.currentTimeMillis() + "code.png";

        //创建文件,安卓低版本的方式
       //  File file=new File(Environment.getExternalStorageDirectory() +"/test.png");

        //Android Q  10为每个应用程序提供了一个独立的在外部存储设备的存储沙箱,没有其他应用可以直接访问您应用的沙盒文件
        File f = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File file = new File(f.getPath() + "/"+imageName);//创建文件
      //        file.getParentFile().mkdirs();
        try {
            //文件输出流
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            //压缩图片,如果要保存png,就用Bitmap.CompressFormat.PNG,要保存jpg就用Bitmap.CompressFormat.JPEG,质量是100%,表示不压缩
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            //写入,这里会卡顿,因为图片较大
            fileOutputStream.flush();
            //记得要关闭写入流
            fileOutputStream.close();
            //成功的提示,写入成功后,请在对应目录中找保存的图片
            Log.e("写入成功!位置目录", f.getPath() + "/"+imageName);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            //失败的提示,这里的Toast工具类,大家用自己项目中的即可,若不需要可以删除
            ToastUtil.showToast(e.getMessage());

        } catch (IOException e) {
            e.printStackTrace();
            //失败的提示
            ToastUtil.showToast(e.getMessage());
        }

        // 下面的步骤必须有,不然在相册里找不到图片,若不需要让用户知道你保存了图片,可以不写下面的代码。
        // 把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(activity.getContentResolver(),
                    file.getAbsolutePath(), imageName, null);
            ToastUtil.showToast( "保存成功,请您到 相册/图库 中查看");
        } catch (FileNotFoundException e) {
            ToastUtil.showToast( "保存失败");
            e.printStackTrace();
        }
        // 最后通知图库更新
        activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                Uri.fromFile(new File(file.getPath()))));

    }

Note:不会获取图片Bitmap 参数的同学可以参考以下方式:
(下面的imageView是你的布局文件中的ImageView的id)
1)方式一:如果你的图片来自于网络,该 url 以图片的形式已经显示在app上了,此时需要保存这张图片时,将图片转换成Bitmap

Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

2)方式二:如果你的图片是app本地的图片,需要保存这张图片时,将图片转换成Bitmap
若ImageView的属性设置了 android:background="@drawable/qr_code",使用下面的方式

Bitmap bitmap=((BitmapDrawable)(imageView.getBackground())).getBitmap();

若ImageView的属性设置了 android:src="@drawable/qr_code",使用下面的方式

 Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

三、示例(仅供参考):

 


版权声明:本文为博主原创文章,转载请点赞此文并注明出处,谢谢!

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值