No activity found to handle intent{act=android,intent.action.VIEW}

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
java
android
android-studio
asked on Stack Overflow Nov 11, 2020 by Amit Bhatiwal • edited Nov 11, 2020 by Amit Bhatiwal

1 Answer

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);
answered on Stack Overflow Nov 11, 2020 by Amit Bhatiwal

User contributions licensed under CC BY-SA 3.0