0

I have an image located at path temp_path .i want that image to be opened with Android gallery app .To do so i add following code inside a function in Fragment

File file=new File(temp_path);
Uri uri;
if(Build.VERSION.SDK_INT<24)
{
   uri=Uri.fromFile(file);
}
else
  {
     uri=Uri.parse(file.getPath());
  }
  Intent intent=new Intent(Intent.ACTION_VIEW);
  intent.setDataAndType(uri,"jpg/jpeg/png");
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intent);

But i am getting following exception

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=/storage/emulated/0/Android/data/com.example.force/files/Pictures/temp_image.jpg typ=jpg/jpeg/png flg=0x10000000 }

How to resolve this exception.

Tried Solution 1(adding fileprovider) I added provider into AndroidManifest.xml

<provider
    android:authorities="${applicationId}.provider"
    android:name="androidx.core.content.FileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_path"/>
</provider>

Also created provider_path.xml as

<?xml version="1.0" encoding="utf-8"?>
<paths
    xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="."/>
</paths>

and also modified my java code to

    File file=new File(temp_path);
    Uri uri;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
    {
        uri = FileProvider.getUriForFile(getContext() ,getContext().getPackageName() + ".provider", file);
    } else
    {
        uri = Uri.fromFile(file);
    }
    Intent intent=new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri,"jpg/jpeg/png");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

But still getting the same Exception

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.example.force.provider/external_files/Android/data/com.example.force/files/Pictures/temp_image.jpg typ=jpg/jpeg/png flg=0x10000000 }

note value of dat :is it right?

my image is located at path /Android/data/com.example.force/files/Pictures/temp_image.jpg in external storage

Tried Solution 2: I added provider into AndroidManifest.xml

<provider
    android:authorities="${applicationId}.provider"
    android:name="androidx.core.content.FileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_path"/>
</provider>

Also created provider_path.xml

Modified as compared to tried solution 1

<paths
    xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="com.example.force"
        path="."/>
</paths>

and also modified my java code to as compared to tried solution 1

    File file=new File(temp_path);
    Uri uri;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
    {
        uri = FileProvider.getUriForFile(getContext() ,getContext().getPackageName() + ".provider", file);
    } else
    {
        uri = Uri.fromFile(file);
    }
String mime="/*";
                    MimeTypeMap mimeTypeMap=MimeTypeMap.getSingleton();
                    if(mimeTypeMap.hasExtension(mimeTypeMap.getFileExtensionFromUrl(uri.toString())))
                    {
                        mime=mimeTypeMap.getMimeTypeFromExtension(mimeTypeMap.getFileExtensionFromUrl(uri.toString()));
                }

    Intent intent=new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri,mime);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

this time no exception arise but gallery app is displaying File format isn't supported or files are corrupted and if i see detail of image its picking path of image as

`content://com.example.force.provider/com.example.force./Android/data/com.example.force/files/Pictures/temp_image.jpg`

but right path of image file is

/Android/data/com.example.force/files/Pictures/temp_image.jpg
6
  • 2
    You should be crashing with a FileUriExposedException. Please use FileProvider and its getUriForFile() for obtaining the Uri to use. Also, please use a valid MIME type. It is your content, so you are responsible for stating what the MIME type is of that content. Nov 11, 2020 at 14:31
  • @CommonsWare i tried using file provider check in Tried Solution 1 and exception it throws Nov 11, 2020 at 16:59
  • 1
    Did you fix the MIME type? jpg/jpeg/png is not a valid MIME type, and there will be approximately zero apps on the planet that support it. Nov 11, 2020 at 17:00
  • @CommonsWare check tried solution 2 Nov 11, 2020 at 18:14
  • "but right path of image file is" -- you are sending a Uri, not a path. Add FLAG_GRANT_READ_URI_PERMISSION to your Intent as a flag. Nov 11, 2020 at 18:18

1 Answer 1

0

Thanks to Intellij Amiya and CommonsWare for helping . Final solution to this problem is achieved by adding tried solution 2 and also adding flag to intent

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.