意图显示空白图像

Cos*_*nci 5 android android-intent

当我尝试使用意图在图库上打开图像(我正在开发文件管理器)时,显示的图像完全是黑色的.但我无法理解为什么,因为我也遵循谷歌最后的最佳做法关于获取Uri(使用FileProvider).

MainActivity.java

Intent intent=new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    Uri uri=FileProvider.getUriForFile(MainActivity.this,BuildConfig.APPLICATION_ID + ".provider",image);
                    intent.setDataAndType(uri,MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileName));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</application>
Run Code Online (Sandbox Code Playgroud)

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
   <paths xmlns:android="http://schemas.android.com/apk/res/android">
      <external-path name="external_files" path="."/>

</paths>
Run Code Online (Sandbox Code Playgroud)

Com*_*are 10

更改:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Run Code Online (Sandbox Code Playgroud)

至:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_GRANT_READ_URI_PERMISSION);
Run Code Online (Sandbox Code Playgroud)

就像现在一样,另一个应用程序无权查看由...识别的图像Uri.

  • @AADTechnical:例外情况应该在另一个应用程序中,而不是 OP 的应用程序中。 (2认同)