0

I wanted to view my recently downloaded image with my default gallery app in my Android 8.0 [Oreo].

I have been using intent for that, but it shows notification as "Media not found".

After doing bit search on it, I found that, for "Android N and above" we need to use "FileProvider" but I did not found good explanatory post for that, so please help me on it.....

Please help me by either using intent or file provider:

My code is as follows:

String root = Environment.getExternalStorageDirectory().toString();

public void onClick(DialogInterface dialog, int id)
{
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse(root + "/h_jokes_images/"+fname+".jpg"), "image/mime");
    startActivity(intent);
}
prasad_21
  • 117
  • 2
  • 18

1 Answers1

1

if your app targets Android N (7.0) and above, you must use a ContentProvider.

Intent intent = new Intent(Intent.ACTION_VIEW)//
                                    .setDataAndType(VERSION.SDK_INT >= VERSION_CODES.N ?
                                                    android.support.v4.content.FileProvider.getUriForFile(this,getPackageName() + ".provider", file) : Uri.fromFile(file),
                            "image/*").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

manifest:

<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>

res/xml/provider_paths.xml :

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <!--<external-path name="external_files" path="."/>-->
    <external-path
        name="files_root"
        path="Android/data/${applicationId}"/>
    <external-path
        name="external_storage_root"
        path="."/>
</paths>
Sultan Mahmud
  • 1,245
  • 8
  • 18