Due to the storage access changes that were made on Android 11, SDK 30, I have changed the path where I save my PDF files and images.
Before, I was using this:
File file = new File(Environment.getExternalStorageDirectory() + "/" + folderName + "/" + fileName);
Here is the file provider:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
Here is the provider_paths.xml before the change of the path:
<paths>
<external-path name="external_files" path="myFolder/"/>
<files-path name="files" path="docs/" />
</paths>
Now, I have changed the path in:
File file = new File(context.getExternalFilesDir(null) + "/" + folderName + "/" + fileName);
The PDF files and the images are saved successfully.
But when I try to share the PDF from my PDFView in the app, the application crashes at the FileProvider.getUriForFile(...)
. It was working fine before I changed the path.
case R.id.action_share:
Intent intentShare = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri u=FileProvider.getUriForFile(getBaseActivity(), AUTHORITY, file);
intentShare = new Intent(Intent.ACTION_SEND);
intentShare.setType("application/pdf");
intentShare.putExtra(Intent.EXTRA_STREAM, u);
intentShare.putExtra(Intent.EXTRA_SUBJECT, "Sharing File...");
intentShare.putExtra(Intent.EXTRA_TEXT, "Sharing File...");
intentShare.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intentShare);
}
getBaseActivity()
returns the context
AUTHORITY is "[name of package].provider"
I have changed the File Provider many times, trying many combinations, including the following (I put all separately not all at once) and nothing seems to work..
<external-files-path name="external_files" path="." />
<external-files-path name="external_files" path="/" />
<external-files-path name="my_folder" path="myFolder/" />
<external-path name="my_folder" path="Android/data/[name of the package]/files/myFolder" />
<files-path name="files" path="." />
<external-files-path name="external_files" path="." />
I keep getting this error and I cannot find a suitable solution, I've been stuck here for 3 weeks...
Here are the logcat lines:
2021-12-09 15:09:09.771 23495-23495/[package name] k E/AndroidRuntime: FATAL EXCEPTION: main Process: [package name], PID: 23495 java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/[package name]/files/myFolder/879881480803.pdf at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744) at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418) at hr.asseco.ui.activity.fragment.PdfFragment.onOptionsItemSelected(PdfFragment.java:145) at androidx.fragment.app.Fragment.performOptionsItemSelected(Fragment.java:2733) at androidx.fragment.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManagerImpl.java:2758) at androidx.fragment.app.FragmentController.dispatchOptionsItemSelected(FragmentController.java:411) at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:390) at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:228) at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109) at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109) at androidx.appcompat.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:65) at androidx.appcompat.widget.Toolbar$1.onMenuItemClick(Toolbar.java:207) at androidx.appcompat.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:779) at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834) at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158) at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985) at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:975) at androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.java:623) at androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:151) at android.view.View.performClick(View.java:7161) at android.view.View.performClickInternal(View.java:7138) at android.view.View.access$3500(View.java:811) at android.view.View$PerformClick.run(View.java:27419) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:221) at android.app.ActivityThread.main(ActivityThread.java:7542) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)