简体   繁体   中英

Open file with pdf if file exists

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! :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM