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