I don't know how to write a code which check if pdf file exists and if it true, the another program open it. For example, my app user press on the button and then on the screen appears small GUI window with two selections : download PDF file or open it with another program. If user press "Open PDF" and pdf file isn't exists, it download automaticaly or on the screen appears a snackbar with text like: "You must to download file". Maybe you can write me a code or maybe it can be done easier? So, my problem - I don't know, how to write that program open a pdf file. Thanks! :)
Asked
Active
Viewed 151 times
1 Answers
0
To open any file type you can use the particular Intent like below
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(path), "pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}catch (ActivityNotFoundException e){
e.printStackTrace();
}
If user don't have any app to handle the pdf files it will enter into catch block and you can handle that case as well

Gautam
- 3,252
- 3
- 23
- 32
-
Pair that with the code from this other [answer](http://stackoverflow.com/a/16238204/924) and you have all the code to check if a file exists before asking another app to open it. – Brandon Haugen Apr 05 '16 at 01:16
-
I did File file = File(fullPath); if (await file.exists() && Platform.isAndroid) { OpenFile.open(file.path); // viewPdf(); } else { StateManager.download2(dio, imgUrl, fullPath); } – wolfsoft Developer Feb 08 '23 at 03:38
-
But OpenFile.open(file.path) shows all apps to view pdf even photos, messanger everything. – wolfsoft Developer Feb 08 '23 at 03:39
-
It should show only pdf supported files – wolfsoft Developer Feb 08 '23 at 03:39