Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gallery picker在Android 10上返回带有文件方案的Uri,如何读取?_Android_Kotlin_Android Contentresolver_Android 10.0_Android External Storage - Fatal编程技术网

Gallery picker在Android 10上返回带有文件方案的Uri,如何读取?

Gallery picker在Android 10上返回带有文件方案的Uri,如何读取?,android,kotlin,android-contentresolver,android-10.0,android-external-storage,Android,Kotlin,Android Contentresolver,Android 10.0,Android External Storage,我的应用程序具有targetSdkVersion=29(新作用域存储策略),并且在清单中未启用requestlegacyexternalstorage标志(我不想启用它) 我试图表达意图的方式: Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChoo

我的应用程序具有targetSdkVersion=29(新作用域存储策略),并且在清单中未启用requestlegacyexternalstorage标志(我不想启用它)

我试图表达意图的方式:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
我也试过这个

val galleryIntent = Intent(Intent.ACTION_PICK).apply {
    type = PICK_INTENT_TYPE
}
flowInterruptBlocker?.blockFlowInterruption()
startActivityForResult(galleryIntent, GALLERY_INTENT_REQUEST_CODE)
还尝试将
类别\u OPENABLE
添加到意图中。这些都不管用

因此,当我发送从gallery中挑选图像的意向时,play market的一些应用程序会返回一个带有文件方案的Uri,例如:file:///storage/emulated/0/Download/sample.jpg

当我尝试将其转换为位图(用于设置为imageView)时,会出现EACCES(权限被拒绝)异常

我试着用一种:

java.File Api

contentResolver.openInpuStream()

contentResolver.openFileDescriptor()

ImageDecoder
没什么帮助。正如我所看到的,如果清单中没有启用requestlegacyexternalstorage标志,我就无法读取android 10(针对29 sdk)上文件方案的Uri

如果我错了,请帮我弄清楚怎么读

神秘的是,android 11 emulator和应用程序目标29 sdk可以在没有requestlegacyexternalstorage标志的情况下读取这些类型的Uri

是的,我已授予读/写外部存储权限

private static Bitmap readImage(Uri uri, Context context){
   InputStream inputStream = context.getContentResolver().openInputStream(uri);
   Bitmap res = BitmapFactory.decodeStream(inputStream);
   inputStream.close();
   return res;
}
play market的一些应用程序会返回带有文件方案的Uri,例如:file:///storage/emulated/0/Download/sample.jpg

有些应用程序是由经验不足的开发人员编写的

如果我错了,请帮我弄清楚怎么读

在Android 10上,如果没有Android:requestLegacyExternalStorage=“true”,您将无法读取
Uri

神秘的是,android 11 emulator和应用程序目标29 sdk可以在没有requestlegacyexternalstorage标志的情况下读取这些类型的Uri


Android 11中引入的“原始路径”功能意味着
READ\u EXTERNAL\u STORAGE
再次正常工作。因此,推荐的方法是请求
读取外部存储
并使用
android:requestLegacyExternalStorage=“true”
,以确保android版本之间的一致性。

这对我来说非常适合(我已经在android 8、10、11上测试了它,目标是sdk 29)。不需要android:requestLegacyExternalStorage=“true”和任何权限

private static Bitmap readImage(Uri uri, Context context){
   InputStream inputStream = context.getContentResolver().openInputStream(uri);
   Bitmap res = BitmapFactory.decodeStream(inputStream);
   inputStream.close();
   return res;
}
你是这样使用它的:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
       
    if(data == null || data.getClipData().size() == 0)
       return;

    myImageView.setImageBitmap(readImage(data.getClipData().getItemAt(0).getUri(), this));
                    
}

请以一个代码块开始您的文章,其中包含用于已用目的的已用代码。
file:///storage/emulated/.../image.jpeg
请给出完整路径。
s我可以看到,我无法读取android 10上文件模式的Uri
可以,但这取决于完整路径。这就是我问的原因:安卓11的限制性更小。那你就有很多途径了。谷歌退了一步。这类来自play marked的应用程序使用类似getRealPathFromUr()的东西,或者是旧的文件浏览器应用程序。。大多数情况下,该功能在Android 10上不可用,但显然,他们已经请求了遗留存储权限,或者想出了一些代码来确定文件路径。不幸的是,在安卓10上无法使用。怪那些应用程序的作者。(你可以邮寄)。请说出他们的名字!