1

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);
devtry
  • 11
  • 3
  • 1
    You should post complete code for your ACTION_VIEW intent. – blackapps Feb 28 '21 at 18:23
  • `I use "file.setReadable(true,false)" to set the rights` That makes no sense. – blackapps Feb 28 '21 at 18:24
  • 1
    getFilesDir() is a folder private to your app. No other app has access. Still you can serve your file from there. – blackapps Feb 28 '21 at 18:26
  • i just have a new class to open the file: – devtry Mar 01 '21 at 17:19
  • Read my first comment. – blackapps Mar 01 '21 at 17:32
  • I just update my post in the last minutes. THANKS! – devtry Mar 01 '21 at 17:33
  • On what kind of device are you testing? A device with Android before version N? – blackapps Mar 01 '21 at 18:13
  • String fname = getContext().getFilesDir().getPath() + File.separator + "download.pdf"; – devtry Mar 01 '21 at 20:06
  • I run this on a Samsung A70 with Android Version 10. – devtry Mar 01 '21 at 20:07
  • The filename was build in MainActivity when the "Download" button was pressed, then it called the "PRDownloader.download(...)" and the open file was done in the "onDownloadComplete" method. – devtry Mar 01 '21 at 20:32
  • `Uri.parse(fname)` That will not give a valid uri as you have seen. Normally it goes wrong as programmers use Uri.fromFile(file);. You do it pretty original. You have no FileUriExposedException but still you should use a FileProvider to serve your file. – blackapps Mar 01 '21 at 21:20
  • Yes, now I get an FileUriExposeException... Thanks for the hint. How to rewrite this piece of code? How to use a FileProvider? Like here: https://stackoverflow.com/questions/18249007/how-to-use-support-fileprovider-for-sharing-content-to-other-apps – devtry Mar 02 '21 at 06:23
  • I found this article about FileProvider: https://ali-dev.medium.com/open-a-file-in-another-app-with-android-fileprovider-for-android-7-42c9abb198c1 I will check this later, *THANKS* to blackapps for the hint! – devtry Mar 03 '21 at 06:21
  • Hey.. I got it! The new code with the FileProvider and changes to "AndroidManifest" and "filepath.xml" works on my real android mobile phone: the button download a PDF from a URL, put it into the private files area and opens a dialog "Open with".. THANKS THANKS THANKS to @backapps : you saved my day and parts of my android developer life.... – devtry Mar 04 '21 at 11:33

0 Answers0