Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent shows a blank image

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>
like image 489
Cosimo Sguanci Avatar asked Sep 12 '16 12:09

Cosimo Sguanci


People also ask

How do you open camera through intent and display captured image?

Intent camera_intent = new Intent(MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult(camera_intent, pic_id); Now use onActivityResult() method to get the result, here the captured image.


1 Answers

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.

like image 138
CommonsWare Avatar answered Sep 29 '22 01:09

CommonsWare