I just try to play with the PRDownloader class to get a PDF from the Internet. This works good in the VDI (I see the file in the Device File Explorer). But now I want to test this on a real Android phone and let the Acrobat Reader open the PDF.
I use "getFilesDir()" as path to save the document, this seems to work (file.exist()). But when I start an Intent to open this file with a PDF Viewer, the Acrobar Reader opens very short and reports (as a Toast) "Cannot access this file".
I use "file.setReadable(true,false)" to set the rights, and I see (also on the read device) the rights "-rw-r--r--" to the file.
How/where to set the rights for other applications to read this file/folder?
Thanks
Steffen
--
I use this code in "MainActivity::onDownloadComplete" ('String fname' is the complete filename)
String fname = getContext().getFilesDir().getPath() + File.separator + "download.pdf";
....
File file = new File(fname);
String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension(ext);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(fname), type);
file.setReadable(true, false);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I also tried to save to file to download folder, but this (also) failed (no file was written):
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()
--[2021-03-02 07:20]--
I extracted and merge all the code to another button onClick:
public void onClick(View v) {
String url = "https://www.antennahouse.com/hubfs/xsl-fo-sample/pdf/basic-link-1.pdf";
String dir = getContext().getFilesDir().getPath();
String bname = "download.pdf";
int downloadId = PRDownloader.download(url, dir, bname)
.build()
.start(new OnDownloadListener() {
@Override
public void onDownloadComplete() {
try {
File file = new File(dir + File.separator + bname);
String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension(ext);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), type);
file.setReadable(true, false);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
catch (FileUriExposedException e) {
Log.e("Download", e.toString());
}
}
@Override
public void onError(Error error) {
}
});
}
----[2021-03-04]----
file "res\xml\filepath.xml"
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="external_files" path="."/>
</paths>
AndroidManifest.xml
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.test.downloadfile.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepath" />
</provider>
</application>
MainActivity:
String dir = getContext().getFilesDir().getPath();
String bname = "download.pdf";
....
....
File downloadPath = new File(dir);
File downloadFile = new File(downloadPath, bname);
String ext = downloadFile.getName().substring(downloadFile.getName().lastIndexOf(".") + 1);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension(ext);
Context context = MainActivity.this;
final Uri data = FileProvider.getUriForFile(context, "com.test.downloadfile.fileprovider", downloadFile);
context.grantUriPermission(context.getPackageName(), data, Intent.FLAG_GRANT_READ_URI_PERMISSION);
final Intent intent = new Intent(Intent.ACTION_VIEW)
.setDataAndType(data, type)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);