I was trying to create a chooser to grant users freedom to open a pdf file with the application they prefer.
Here's my approach:
private void openPdf(String pdfUrl, String mime) {
// Intent pdfViewIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
Intent pdfViewIntent = new Intent(Intent.ACTION_VIEW);
pdfViewIntent.setDataAndType(Uri.parse(pdfUrl), mime);
Log.d(TAG, "openPdf: uri: " + Uri.parse((pdfUrl)));
Intent chooser = Intent.createChooser(pdfViewIntent, "Choose an app");
// chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // optional
startActivity(chooser);
finish();
}
The pdf url is something like this: https://drive.google.com/file/d/.../view?usp=sharing
And for the mime type, I tried using both "application/vnd.google-apps.document"
and "application/pdf"
.
Now, when the user clicks on document, I get an output like the following:
But I want an output like the following screenshot: (it appears when I try to open the same link with WhatsApp)
NOTE 1: using WebView is not a good option for me.
NOTE 2: the above approach works if the URL is in the format https://example.com/.../filename.pdf
. But it fails for Google Drive's link.
Intent.FLAG_GRANT_READ_URI_PERMISSION
– Ferocitywas trying to create a chooser to grant users freedom to open a pdf file...
That has nothing to do with a chooser but with using an ACTION_VIEW intent. – Aladdinhttps://our.domain.name/relativeurl/filename.pdf
the above solution works perfectly. This solution fails only when google drive's url is provided. – Snuffer/preview
instead of/view
at the end of your URL or generate a direct download link for the google drive file of the formathttps://drive.google.com/uc?export=download&id=DRIVE_FILE_ID
where DRIVE_FILE_ID are the letters between/d/
and/view
. – Porcine