iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android 图片保存到相册不显示的解决方案(兼容Android 10及更高版本)
  • 547
分享到

Android 图片保存到相册不显示的解决方案(兼容Android 10及更高版本)

2024-04-02 19:04:59 547人浏览 泡泡鱼
摘要

目录前言问题解决问题前言 写了一个demo,简单逻辑就是:在一个图片上添加一行文字或者是水印,并且保存到系统相册,也就是我们手机上的图库。前面编辑图片添加水印都没有问题,到后面保存

前言

写了一个demo,简单逻辑就是:在一个图片上添加一行文字或者是水印,并且保存到系统相册,也就是我们手机上的图库。前面编辑图片添加水印都没有问题,到后面保存到系统相册出现了问题:显示不出来图片。

问题

Android 10 之前保存系统相册的三步骤:

  • 保存图片到手机
  • 把图片插入到手机图库
  • 发广播更新

代码如下:


public static void savePhotoAlbum(Context context, Bitmap bmp) {
    // 首先保存图片
    File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
    if (!appDir.exists()) {
        appDir.mkdir();
    }
    String fileName = System.currentTimeMillis() + ".jpg";
    File file = new File(appDir, fileName);
    try {
        FileOutputStream fos = new FileOutputStream(file);
        bmp.compress(CompressFORMat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
	}
    
    // 其次把文件插入到系统图库
    try {
        MediaStore.Images.Media.insertImage(context.getContentResolver(),
				file.getAbsolutePath(), fileName, null);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    // 最后通知图库更新
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
}

出现的问题:图片不显示,也就是说没有更新到系统图库中。

细心的小伙伴会发现,上段代码有两处地方废弃的方法:


 MediaStore.Images.Media.insertImage(context.getContentResolver(),
				file.getAbsolutePath(), fileName, null);

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));

解决问题

下面是解决上面的问题,并兼容 Android10 版本:


    
    private void imgMerge() {
        new Thread(() -> {
            try {
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
                File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "newFile.jpg");
                if (!file.exists()) {
                    file.createNewFile();
                }
                //添加水印文字位置。
                Bitmap newBitmap = addTextWatermark(bitmap, "测试demo示例");
                //保存到系统相册
                savePhotoAlbum(newBitmap, file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }
    
    
    private void savePhotoAlbum(Bitmap src, File file) {
        if (isEmptyBitmap(src)) {
            return;
        }
        //先保存到文件
        OutputStream outputStream;
        try {
            outputStream = new BufferedOutputStream(new FileOutputStream(file));
            src.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            if (!src.isRecycled()) {
                src.recycle();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //再更新图库
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DISPLAY_NAME, file.getName());
            values.put(MediaStore.MediaColumns.MIME_TYPE, getMimeType(file));
            values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM);
            ContentResolver contentResolver = getContentResolver();
            Uri uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,  values);
            if (uri == null) {
                return;
            }
            try {
                outputStream = contentResolver.openOutputStream(uri);
                FileInputStream fileInputStream = new FileInputStream(file);
                FileUtils.copy(fileInputStream, outputStream);
                fileInputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            MediaScannerConnection.scanFile(
                    getApplicationContext(),
                    new String[]{file.getAbsolutePath()},
                    new String[]{"image/jpeg"},
                    (path, uri) -> {
                        // Scan Completed
                    });
        }
    }

发送广播和插入MediaProvider两种方式添加图片到相册,这两种方式已经官方废弃了。在 Android 10版本以及更高版本使用上面的方法,才能有效解决不显示图片的问题。

做个记录!

以上就是Android 图片保存到系统相册不显示的解决方案(兼容Android 10及更高版本)的详细内容,更多关于Android 图片保存到相册不显示的资料请关注编程网其它相关文章!

--结束END--

本文标题: Android 图片保存到相册不显示的解决方案(兼容Android 10及更高版本)

本文链接: https://www.lsjlt.com/news/124224.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作