Android Fileprovider:无法找到包含

时间:2017-10-10 18:37:03

标签: android android-fileprovider

我创建了一个创建gpx文件的应用程序。除了分享之外,一切都很好。 因此我创建了一个文件提供程序。你可以在下面看到它的配置。提供商在运行Android 8.0.0的Android设备上工作正常,但在华为(6.0)的朋友上它无法正常工作

Fatal Exception: java.lang.IllegalArgumentException
Failed to find configured root that contains /storage/8737-15E4/Android/data/XXX/cache/20171009_171900.gpx

Manifest中的提供者:

<provider
        android:name=".GenericFileProvider"
        android:authorities="com.package.test.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

file_paths.xml:

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

代码中的用法:

File gpxFile = new File(context.getExternalCacheDir(), "20171009_171900.gpx");    
Uri gpxContentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", gpxFile);

        Intent gpxIntent = new Intent(Intent.ACTION_SEND);
        gpxIntent.setType("text/gpx");

        gpxIntent.putExtra(Intent.EXTRA_STREAM, gpxContentUri);

        Intent programChooser = Intent.createChooser(gpxIntent, context.getString(R.string.select_app_to_share));
        programChooser.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

        activityForDialog.startActivity(programChooser);

我希望有人可以帮助我找到导致应用在某些设备上崩溃的错误......

1 个答案:

答案 0 :(得分:2)

修改您在代码中的用法:&#34;并替换第二行

Uri gpxContentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", gpxFile);

用这个:

Uri gpxContentUri;
try {
    gpxContentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", gpxFile);
} catch (IllegalArgumentException e) {
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
    gpxContentUri = Uri.fromFile(gpxFile);
}

注意:这个错误似乎只是在华为P8 Lite(PRA-LX1)&#34;运行Android 7.0,Mojo说它只发生在他朋友的华为(6.0)上。我开始认为这只是这些手机的一个问题,但有一个解决方法很好。