If your application is designed for Android N (7.0) and higher, you should not use the answers above (the "Uri.fromFile" method), because it will not work for you.
Instead, you should use ContentProvider.
For example, if your image file is in an external folder, you can use this (similar to the code I made here ):
File file = ...; final Intent intent = new Intent(Intent.ACTION_VIEW)// .setDataAndType(VERSION.SDK_INT >= VERSION_CODES.N ? FileProvider.getUriForFile(this,getPackageName() + ".provider", file) : Uri.fromFile(file), "image/*").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
manifest:
<provider android:name="androidx.core.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>
res / xml / provider_paths.xml:
<?xml version="1.0" encoding="utf-8"?> <paths> <external-path name="files_root" path="Android/data/${applicationId}"/> <external-path name="external_storage_root" path="."/> </paths>
If your image is in the personal path of the application, you must create your own ContentProvider, as I created "OpenFileProvider" by reference.
android developer Dec 31 '16 at 20:17 2016-12-31 20:17
source share