I have some PDF files stored in /storage/emulated/0/Name123
directory like:
/storage/emulated/0/Name123/Key1/randomName1.pdf
/storage/emulated/0/Name123/Key2/randomName2.pdf
/storage/emulated/0/Name123/Key3/randomName3.pdf
and so on
Considering these files, my java android-studio-based application has a button that creates an ACTION_VIEW
Intent
. The idea is to allow users to open the PDF file with the PDF viewer/editor application of the user's choice.
Everything works great except for one thing: all the changes and edits to the PDF file do not reflect on the actual file residing on the filesystem. So, if you click on the button, open the PDF and highlight some lines on a page, save it, and close the PDF reader application and get back to our application and click on the button to open the same file again, the same PDF without all those changes is going to be opened.
I have noticed a similar question without any answers on Stackoverflow.
By the way, I do not want to use solutions like the code below to avoid FileUriExposedException
caused by supplying a file URI
to an Intent
on the new versions of Android:
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Is there any other way to launch another independent application to open a PDF file in a certain absolute path
preserving the write access to the file?
Given the new restrictions for file URIs on new Android versions, how can the file manager apps launch third-party apps to open files, as I confirm that I can open a PDF file with X-plore, edit it, and save it back on the actual input file on the filesystem with the same PDF reader apps that fail to gain write access to my content URI, like Adobe Reader, Foxit Reader, XODO.
Here are my codes (Following this topic to use FileProvider):
File: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<application
...
<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>
</application>
</manifest>
File: xml/provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
File: MainActivity.java Button's onClickListener
String key = "Key2/randomName2.pdf"
File path = new File("/storage/emulated/0/Name123");
File file = new File(path, key);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(getApplicationContext(),getApplicationContext().getPackageName()+".provider", file);
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(intent, null);
// The application has the required permissions to access and manage ALL FILES on the external memory.