Reputation: 405
I have created an app, that creates gpx files. Everything is working fine except for the sharing. Therefore I have created a File Provider. You can see it's configuration below. The Provider is working fine on my android device running Android 8.0.0 but on a friends Huawei (6.0) it is not working
Fatal Exception: java.lang.IllegalArgumentException
Failed to find configured root that contains /storage/8737-15E4/Android/data/XXX/cache/20171009_171900.gpx
Provider in 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>
Usage in Code:
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);
I hope someone can help me to find the bug that causes the app to crash on some devices...
Upvotes: 6
Views: 2714
Reputation: 3176
Modify your "Usage in Code:" and replace the 2nd line
Uri gpxContentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", gpxFile);
with this:
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);
}
Note: This error only seemed to be thrown for me on the "Huawei P8 Lite (PRA-LX1)" running Android 7.0, and the Mojo said it only happened on his friend's Huawei (6.0). I'm starting to think this is only an issue with those phones, but it's good to have a workaround.
Upvotes: 4