简体   繁体   中英

Open external PDF file on android emulator

I tried to open PDF files that have external links, but after Adobe reader is opening, it gives me that "The document path is not valid"

This is the code I wrote:

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                File file = new File("http://www.kb.nl/sites/default/files/docs/pdf_guidelines.pdf");
                                Uri path = Uri.fromFile(file);
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                intent.setDataAndType(path,"application/pdf");
                                try 
                                {
                                    startActivity(intent);
                                } 
                                catch (ActivityNotFoundException e) 
                                {
                                    Toast.makeText(MainActivity.this, 
                                        getString(R.string.app_name), 
                                        Toast.LENGTH_SHORT).show();
                                }
            }
        });

How can I open external PDF files in my emulator, my aim is to open pdf files which are inside my google drive.

Thanks in advance.

File file = new File("http://www.kb.nl/sites/default/files/docs/pdf_guidelines.pdf");

http://www.kb.nl/sites/default/files/docs/pdf_guidelines.pdf is not a path to a File . It is a URL to a Web site. You cannot wrap a Web URL in a File object and expect it to work.

You can try using Uri.parse("http://www.kb.nl/sites/default/files/docs/pdf_guidelines.pdf") to get your Uri and seeing if Adobe Reader will support that, but I doubt it.

More likely, you need to download the PDF yourself, then use that File in the Intent .

This few lines do the magic:

try {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("Url to pdf or other")));
            } catch (ActivityNotFoundException e) {
                //do something here
            }

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