How open document pdf/docx with external app in Android
Asked
Active
Viewed 844 times
0
-
1Possible duplicate of [How to open a PDF via Intent from SD card](https://stackoverflow.com/questions/10530416/how-to-open-a-pdf-via-intent-from-sd-card) – Rasel Oct 09 '19 at 04:24
2 Answers
0
Try the below code :
File pdfFile = new File("File Path");
Intent openPdf = new Intent(Intent.ACTION_VIEW);
openPdf.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
fileUri = FileProvider.getUriForFile(viewContext,com.mydomain.fileprovider, pdfFile);
openPdf.setDataAndType(fileUri, "application/pdf");
openPdf.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(openPdf, "Select Application"));
Note : You need File Provider for granting URI permissions check this out

Om Infowave Developers
- 1,525
- 15
- 40
-
I try File pdfFile = new File("/storage/emulated/0/eoffice/1.pdf"); but file canot show in pdf app What is wrong url file ? i acces from internal storage in folder "eoffice/1.pdf" – Sekuntum Rosela Oct 09 '19 at 04:35
-
@SekuntumRosela You need File Provider for granting URI permissions because you are sharing file path to an external application. also, u need to provide full path of file. – Om Infowave Developers Oct 09 '19 at 04:41
-
-
-
i try create file provider but pdf blank screen can't show `
-
@SekuntumRosela did you create the xml file? post the logs it will help to understand. – Om Infowave Developers Oct 09 '19 at 04:51
-
-
@SekuntumRosela can you post your code in question and logs? – Om Infowave Developers Oct 09 '19 at 04:56
0
This will help you
//method to show file chooser
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"), PICK_PDF_REQUEST);
}
You will get the result in onActivity result
//handling the image chooser activity result
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PDF_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
}
}
For more info you can look into https://stackoverflow.com/a/11038348/11834065

Shivam Sharma
- 96
- 5