如何保存Android Q和以后的存储空间中的位图?

发布于 2025-01-20 00:05:02 字数 1481 浏览 3 评论 0原文

在我的应用程序中,我必须将位图作为 PNG 文件存储在共享内存中,以使其对 Gallery 应用程序可见。首先,我尝试将图像存储在 /Android/data/package.name/files/Pictures 中。我从 context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) 获取了此路径。 Gallery 无法检测到存储在此目录中的图像。然后我读了一些关于 MediaStore 的文章和帖子,并尝试用它保存我的图像。

这是我用来存储位图的函数。它不会抛出任何异常,返回 true,bitmap.compress() 也返回 true,但我在设备内存中找不到任何 PNG 图像。我尝试使用图库和文件管理器来查找它。我还尝试将其更改为保存 JPEG 而不是 PNG,但它也不起作用。

您能帮我弄清楚为什么此功能不将图像保存到设备的存储中吗?

我在三星 A52s 5G、Android 12、OneUI 4.0 上进行了测试。

private boolean saveImageToStorageAndroidQ(Bitmap bitmap, String filename) {
    filename = filename + ".png";
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.DISPLAY_NAME, filename);
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
    values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);

    final ContentResolver resolver = getActivity().getContentResolver();
    final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    Uri uri = resolver.insert(contentUri, values);

    try {
        OutputStream imageOutStream = resolver.openOutputStream(uri);
        bitmap.compress(Bitmap.CompressFormat.PNG, 95, imageOutStream);
        imageOutStream.flush();
        imageOutStream.close();
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        if (uri != null)
            resolver.delete(uri, null, null);
    }
}

In my application, I have to store a bitmap as a PNG file in shared memory, to make it visible for Gallery app. First, I tried to store the image in /Android/data/package.name/files/Pictures. I got this path from context.getExternalFilesDir(Environment.DIRECTORY_PICTURES). Images stored in this directory are not detected by Gallery. Then I read a few articles and SO posts about MediaStore and I tried to save my image with it.

This is a function that I use to store bitmap. It does not throw any exception, returns true, bitmap.compress() also returns true but I can't find any PNG image in device's memory. I tried to look for it using Gallery and file manager. I also tried to change it to save JPEG instead of PNG but it also does not work.

Could you help me to figure out why this function does not save image to device's store?

I tested it on Samsung A52s 5G, Android 12, OneUI 4.0.

private boolean saveImageToStorageAndroidQ(Bitmap bitmap, String filename) {
    filename = filename + ".png";
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.DISPLAY_NAME, filename);
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
    values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);

    final ContentResolver resolver = getActivity().getContentResolver();
    final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    Uri uri = resolver.insert(contentUri, values);

    try {
        OutputStream imageOutStream = resolver.openOutputStream(uri);
        bitmap.compress(Bitmap.CompressFormat.PNG, 95, imageOutStream);
        imageOutStream.flush();
        imageOutStream.close();
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        if (uri != null)
            resolver.delete(uri, null, null);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕嗳→ 2025-01-27 00:05:02

您也可以使用此方法。虽然很长,但更好,因为另一个已被弃用。使用此代码:

String filename = "name.png";
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, filename);

try {
     FileOutputStream out = new FileOutputStream(dest);
     bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
     out.flush();
     out.close();
} catch (Exception e) {
     e.printStackTrace();
}

You can also use this method. Thought it's long, it's better as the other one has been deprecated. Use this code:

String filename = "name.png";
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, filename);

try {
     FileOutputStream out = new FileOutputStream(dest);
     bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
     out.flush();
     out.close();
} catch (Exception e) {
     e.printStackTrace();
}
枯寂 2025-01-27 00:05:02

假设您具有位图格式拍摄的图像,对于Android旧版本(在Android 7.0 Nougat小米上尝试)是可以的:

    String new_Date = c.get(Calendar.DAY_OF_MONTH) + "-"
                            + ((c.get(Calendar.MONTH)) + 1) + "-"
                            + c.get(Calendar.YEAR) + " " + c.get(Calendar.HOUR)
                            + "-" + c.get(Calendar.MINUTE) + "-"
                            + c.get(Calendar.SECOND);    



MediaStore.Images.Media.insertImage(getContentResolver(),bitmap,new_Date,"image:"+new_Date);

现在,对于Android版本Q,则建议使用此方法:

private void saveBitmapImage(Bitmap bitmap, @NonNull String name) throws IOException {
    boolean savedSuccessfully;
    OutputStream fos;

 
     ContentResolver resolver = mContext.getContentResolver();
     ContentValues contentValues = new ContentValues();
     contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
     contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
     contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/"+IMAGES_FOLDER_NAME);
     Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
     fos = resolver.openOutputStream(imageUri);


     savedSuccessfully = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
     fos.flush();
     fos.close();
}

Supposed that you have the image taken in bitmap format, this one is ok for Android older versions (tried on Android 7.0 Nougat Xiaomi) :

    String new_Date = c.get(Calendar.DAY_OF_MONTH) + "-"
                            + ((c.get(Calendar.MONTH)) + 1) + "-"
                            + c.get(Calendar.YEAR) + " " + c.get(Calendar.HOUR)
                            + "-" + c.get(Calendar.MINUTE) + "-"
                            + c.get(Calendar.SECOND);    



MediaStore.Images.Media.insertImage(getContentResolver(),bitmap,new_Date,"image:"+new_Date);

Now, for Android version Q and later this method is suggested:

private void saveBitmapImage(Bitmap bitmap, @NonNull String name) throws IOException {
    boolean savedSuccessfully;
    OutputStream fos;

 
     ContentResolver resolver = mContext.getContentResolver();
     ContentValues contentValues = new ContentValues();
     contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
     contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
     contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/"+IMAGES_FOLDER_NAME);
     Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
     fos = resolver.openOutputStream(imageUri);


     savedSuccessfully = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
     fos.flush();
     fos.close();
}
江城子 2025-01-27 00:05:02

您可以使用一种非常简单的方法:

MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, filename, "Description");

这是不建议的。尝试 this 答案

You can use a very easy method:

MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, filename, "Description");

This is deprecated. Try this answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文