FileProvider为某些Android 8用户引发异常

时间:2019-01-30 22:27:28

标签: android android-fileprovider

我看了几个类似File provider throws exception的示例,但似乎无法让我的文件提供者为所有人服务。我已经在Android 8上对其进行了测试,并且可以正常工作,但是对于某些用户来说,它会抛出IllegalArgumentException。

它看起来像:

Manifest
<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="com.my.app.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/file_paths" />
</provider>

路径文件

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

共享:

File path = context.getExternalCacheDir().getAbsolutePath();
path += filename;
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Shared file");
sendIntent.putExtra(Intent.EXTRA_STREAM, getUriForFile(context, "com.my.app.fileprovider", path));
sendIntent.setType("application/octet-stream");
context.startActivity(Intent.createChooser(sendIntent, context.getString(R.string.strBackup)));

例外:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/3764-3933/Android/data/com.my.app/cache/backup.bin
    at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
...

对于所有用户,文件提供程序路径文件应该看起来像什么?为何它适用于大多数Android 8+用户,但不是全部?我应该使用.还是./之类的路径?

0 个答案:

没有答案
相关问题