Shahid Helal
Shahid Helal

Reputation: 23

How do I open a pdf with content URI on android >= 8?

I am trying to open a pdf with existing installed pdf apps such as Google PDF Viewer. But the PDF screen shows blank.

I created an intent and used ACTION_VIEW filter but when I open in Google PDF it shows just blank screen nothing else even name of the file in Google PDF is not visible only document id (content-provider://..../document/122 <- this id) is shown

String filePath = "content://com.android.providers.downloads.documents/document/346";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(filePath), "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
mActivity.startActivity(Intent.createChooser(intent, "select an app to open this file"));

Upvotes: 1

Views: 1952

Answers (1)

Shahid Kamal
Shahid Kamal

Reputation: 440

setFlags will replace all previous flags on intent. So use addFlags instead -

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

also check if you have long term access to content URI. After Android 4.3 you'll need to ask for persistent permission using takePersistableUriPermission() on content uri you get. Also you'll need to change ACTION_VIEW to ACTION_OPEN_DOCUMENT where you are getting the URI you mentioned for takePersistableUriPermission() to work.

Read the article below for more clarification:

Upvotes: 1

Related Questions