繁体   English   中英

没有找到处理意图的活动{act=android,intent.action.VIEW}

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

我有一个位于路径temp_path的图像。我希望使用 Android 画廊应用程序打开该图像。为此,我在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);

但我收到以下异常

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 }

如何解决此异常。

尝试解决方案 1(添加文件提供程序我将提供程序添加到 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>

还创建了 provider_path.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>

并将我的java代码修改为

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

但仍然得到相同的异常

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 }

注意 dat 的值:对吗?

我的图像位于外部存储中的路径/Android/data/com.example.force/files/Pictures/temp_image.jpg

尝试解决方案 2:我将提供程序添加到 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>

还创建了 provider_path.xml

与尝试的解决方案 1 相比进行了修改

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

并将我的 java 代码修改为与尝试的解决方案 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);

这次没有例外,但画廊应用程序显示不支持文件格式或文件已损坏,如果我看到图像的细节,其图像的拾取路径为

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

但图像文件的正确路径是

/Android/data/com.example.force/files/Pictures/temp_image.jpg

感谢 Intellij Amiya 和 CommonsWare 的帮助。 这个问题的最终解决方案是通过添加尝试过的解决方案 2 并在意图中添加标志来实现的

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM