【问题标题】:Android getUriForFile IllegalArgumentExceptionAndroid getUriForFile IllegalArgumentException
【发布时间】:2016-11-02 17:47:52
【问题描述】:

我用于发送带有 Intent 的文件的代码不适用于所有文件源,我还找不到解决方案:

我的应用程序已注册为打开文件,所以当我在 ES 文件资源管理器中选择一个文件时,意图如下所示:

intent->mData->uriString=/storage/emulated/0/backups/apps/NextGenEndPoint_1.0.apk    

Asus File Manager 中选择同一个文件时,我得到以下意图:

intent->mData->uriString=/file/sdcard/backups/apps/NextGenEndPoint_1.0.apk

这是我的代码:

        // With this path it works. Path example being sent by e.g ES File Explorer
        String fileName = "/storage/emulated/0/backups/apps/PDFViewer.apk";
        // with this path it doesn't. This path is sent by Asus File Explorer
        fileName = "/file/sdcard/backups/apps/PDFViewer.apk";
        final Intent installIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        installIntent.setData(getUriForFile(context, "com.myapp.fileprovider", new File(fileName)));
        installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(installIntent);

我也试过 new File("/file/sdcard/backups/apps/PDFViewer.apk").exists();什么返回 false。

xml:

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

以及清单中的提供者:

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.myapp.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_provider_paths" />
    </provider>

例外:

 11-02 12:21:09.004: E/ACRA(9807): com.mycompany.myapp fatal error : Failed to find configured root that contains /file/sdcard/PDFViewer.apk
 11-02 12:21:09.004: E/ACRA(9807): java.lang.IllegalArgumentException: Failed to find configured root that contains /file/sdcard/PDFViewer.apk
 11-02 12:21:09.004: E/ACRA(9807):  at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(SourceFile:711)
 11-02 12:21:09.004: E/ACRA(9807):  at android.support.v4.content.FileProvider.getUriForFile(SourceFile:400)

我错过了什么?

【问题讨论】:

  • fileName = "/file/sdcard/backups/apps/PDFViewer.apk";。你确定不是fileName = "file:///sdcard/backups/apps/PDFViewer.apk"; 吗?更改为fileName = "/sdcard/backups/apps/PDFViewer.apk";
  • new File(pkg)??什么是pkg?并且没有使用文件名。
  • 路径不包含 file:/// 我对字符串进行了硬编码,因此您可以看到我得到的内容,但这些应用程序的文件提供程序会自动获取这些路径
  • 是的,我知道。所以解决方案非常简单。如果这样的路径以“/file/”开头,则将其替换为“/”,如我之前所说。你为什么不尝试?为什么你对我的建议没有反应?
  • 但是在这样做之前和调用意图之前,您可以使用 File 类来查看 /file/.... 路径是否存在。或者它是华硕的一些“功能”。华硕探索只能通过华硕设备交付?

标签: android android-fileprovider


【解决方案1】:

在引用的文件中:

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

使用文件路径而不是外部路径

使用Environment.getExternalStorageDirectory()

【讨论】:

    【解决方案2】:
     /file/sdcard/backups/apps/NextGenEndPoint_1.0.apk. 
    

    不,这只是您获得的内容方案的一部分。华硕的文件管理器应用程序将提供

     content://com.asus.filemanager.OpenFileProvider/file/sdcard/backups/apps/NextGeEndPoint_1.0.apk. 
    

    您最好在您确定“路径”的位置显示您的代码,因为现在您做错了。

    【讨论】:

    • 但我的问题是 installIntent.setData 并且这使用路径“/storage/emulated/0/backups/apps/NextGenEndPoint_1.0.apk”也没有第一部分内容: //...
    • 确实如此。因为那是 ES File Explorer 提供的有效文件系统路径.. 但是华硕文件管理器并没有给你一个文件系统路径,而是一个内容方案路径。
    【解决方案3】:

    问题在于将 getUriForFile 用于文件系统路径以及实际上必须与内容解析器一起使用的文件提供程序路径,因此解决方案如下:

                Uri uri;
                if (ContentResolver.SCHEME_CONTENT.equals(fileUri.getScheme())) {
                    uri = new Uri.Builder()
                            .path(fileUri.getPath())
                            .authority(fileUri.getAuthority())
                            .scheme(ContentResolver.SCHEME_CONTENT)
                            .build();
                } else {
                    uri = getUriForFile(context, "com.myapp.fileprovider", new File(fileUri.getPath()));
                }
    

    【讨论】:

      猜你喜欢
      • 2018-08-21
      • 2023-03-24
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多