gpt4 book ai didi

android - MediaStore.Images.Media.insertImage - java.io.FileNotFoundException - Android

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:23:25 24 4
gpt4 key购买 nike

Note: similar question is asked over below SO links, but none of the proposed solution worked for me, hence starting this thread.

  1. MediaStore.Images.Media.insertImage is returning null when tryingto save the image
  2. Why Images.Media.insertImage return null

问题陈述:我正在从 url 下载图像,然后尝试共享它。为此,我需要 Uri,这是我通过将图像保存在 MediaStore.Images.Media 中获得的。

代码:

一个。从 AsyncTask 类获取下载的图像

public void processDownloadedImage(Bitmap bitmap) {
if (bitmap == null) {
Toast.makeText(this,"Image could not be downloaded and shared!",Toast.LENGTH_LONG).show();
}
else {
downloadedImage = bitmap;
checkPermissionShowShareSheet();
}
}

B.检查 Marshmallow 及更高版本的权限

public void checkPermissionShowShareSheet() {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(this)){
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},2909);
}
else {


updateImageUri(downloadedImage);
showShareSheet(downloadedImageUri);
}
}
else {
updateImageUri(downloadedImage);
showShareSheet(downloadedImageUri);
}
}

C.获取图片Uri

private void updateImageUri(Bitmap inImage){
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

fixMediaDir();
String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), inImage, "SomeImage", null);
storePathInSharedPreferences(path);
Uri uri = Uri.parse(path);

downloadedImageUri = uri;
}

void fixMediaDir() {
File sdcard = Environment.getExternalStorageDirectory();
if (sdcard != null) {
File mediaDir = new File(sdcard, "DCIM/Camera");
if (!mediaDir.exists()) {
mediaDir.mkdirs();
}
}
}

D. list 文件使用权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

问题:下一行返回的值为空

String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), inImage, "SomeImage", null);

有什么想法吗?

这是 Stackbacktrace 的一部分:

Failed to insert image java.io.FileNotFoundException: No such file or directory       
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:620)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:939)
at android.content.ContentResolver.openOutputStream(ContentResolver.java:686)
at android.content.ContentResolver.openOutputStream(ContentResolver.java:662)
at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:934)
at myorg.myapp.ImageDescription.updateImageUri(ImageDescription.java:165)

最佳答案

改变这个

<uses-permission android:name="android.permission.read_external_storage" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

关于android - MediaStore.Images.Media.insertImage - java.io.FileNotFoundException - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37397404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com