Intent shows a blank image

本秂侑毒 提交于 2019-12-23 09:58:08

问题


When i try to open an image on the gallery (I'm developing a file manager) using intent, the image shown is totally black., but I can't understand why, as I've also followed the last best practices by google about getting the Uri (with 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);

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>

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>

回答1:


Change:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

to:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_GRANT_READ_URI_PERMISSION);

as right now, the other app has no rights to view the image identified by the Uri.



来源:https://stackoverflow.com/questions/39450748/intent-shows-a-blank-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!