简体   繁体   English

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

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

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我有一个位于路径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);

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尝试解决方案 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>

Also created provider_path.xml as还创建了 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>

and also modified my java code to并将我的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);

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?注意 dat 的值:对吗?

my image is located at path /Android/data/com.example.force/files/Pictures/temp_image.jpg in external storage我的图像位于外部存储中的路径/Android/data/com.example.force/files/Pictures/temp_image.jpg

Tried Solution 2: I added provider into AndroidManifest.xml尝试解决方案 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>

Also created provider_path.xml还创建了 provider_path.xml

Modified as compared to tried solution 1与尝试的解决方案 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并将我的 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);

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

Thanks to Intellij Amiya and CommonsWare for helping .感谢 Intellij Amiya 和 CommonsWare 的帮助。 Final solution to this problem is achieved by adding tried solution 2 and also adding flag to intent这个问题的最终解决方案是通过添加尝试过的解决方案 2 并在意图中添加标志来实现的

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

暂无
暂无

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

相关问题 找不到用于处理意图的活动{act = android.intent.action.View} - No activity found to handle intent { act=android.intent.action.View } android.content.ActivityNotFoundException:没有找到处理意图的活动{act=android.intent.action.VIEW dat=PendingIntent{} - android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=PendingIntent{ } 未找到处理 Intent 的活动 (act=android.intent.action.VIEW) 正在尝试安装 APK - No Activity Found to handle Intent (act=android.intent.action.VIEW) Trying to Install APK android.content.ActivityNotFoundException:未找到任何处理Intent的活动{act = android.intent.action.GET_CONTENT - android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.GET_CONTENT 找不到用于处理Intent {的活动“ act = android.intent.action.CALL dat =电话号码:1}? - No Activity found to handle Intent{ act=android.intent.action.CALL dat=Phone number:1 }? 找不到处理 Intent { act=android.intent.action.CALL dat=tel0710000001 } 的活动 - No Activity found to handle Intent { act=android.intent.action.CALL dat=tel0710000001 } 找不到用于处理意图的活动{act = android.intent.action.OPEN_DOCUMENT cat = [android.intent.category.OPENABLE] typ = / *} - No Activity found to handle Intent { act=android.intent.action.OPEN_DOCUMENT cat=[android.intent.category.OPENABLE] typ=/* } 找不到可通过ACTION_VIEW Intent处理Intent的活动 - No Activity found to handle Intent With ACTION_VIEW Intent 找不到活动来处理Intent {act = android.settings.action.MANAGE_WRITE_SETTINGS - No Activity found to handle Intent { act=android.settings.action.MANAGE_WRITE_SETTINGS 错误找不到活动来处理Intent act = android.media.action.IMAGE_CAPTURE - Error No Activity found to handle Intent act=android.media.action.IMAGE_CAPTURE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM