1

我在Android 10 (API 29)设备上运行了两个应用程序。从第一个应用程序(“发件人”)中,我发送了放置在手机文件系统上的文件uri图片:jpg

Intent intent = new Intent(Intent.ACTION_VIEW);        
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
String mimeType = AppUtils.getFileMimeType(uri, context);
intent.setDataAndType(uri, "image/jpeg");
if (intent.resolveActivity(context.getPackageManager()) != null)
    context.startActivity(Intent.createChooser(intent, "Open with"));

在第二个应用程序(“接收器”)中,我尝试InputStream从上方打开uri

context.getContentResolver().openInputStream(uri);

结果得到这个异常:

java.lang.SecurityException: Permission Denial: reading com.android.externalstorage.ExternalStorageProvider uri content://com.android.externalstorage.documents/tree/primary%3AMovies%2FNew/document/primary%3AMovies%2FNew%2FPanorama.jpg from pid=30753, uid=10487 requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
    at android.os.Parcel.createException(Parcel.java:2071)
    at android.os.Parcel.readException(Parcel.java:2039)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
    at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:151)
    at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:705)
    at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1687)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1503)
    at android.content.ContentResolver.openInputStream(ContentResolver.java:1187)

如果这两个应用程序在Android 9 (API 28)设备上运行是没有问题的。Android 10 (API 29)关于通过第三方应用程序访问Intent.ACTION_VIEWContentResolver.openInputStream访问uri第三方应用程序有哪些变化!?

“发件人”应用程序有

compileSdkVersion 30
targetSdkVersion 30

“接收器”应用程序有

compileSdkVersion 29
targetSdkVersion 29

并且android:requestLegacyExternalStorage="true"Manifest.

4

1 回答 1

0

似乎Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION应该intentAndroid 10 (API 29)上发送“Sender”时添加,但Android 9 (API 28)不需要它的原因尚不清楚。

Intent intent = new Intent(Intent.ACTION_VIEW);        
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
String mimeType = AppUtils.getFileMimeType(uri, context);
intent.setDataAndType(uri, "image/jpeg");
if (intent.resolveActivity(context.getPackageManager()) != null)
    context.startActivity(Intent.createChooser(intent, "Open with"));
于 2020-07-15T03:00:40.540 回答