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>
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With