3

I am writing an application that opens a pdf file when you click a button. Below is my code:

File pdfFile = new File(
                        "android.resource://com.dave.pdfviewer/"
                                + R.raw.userguide);
                Uri path = Uri.fromFile(pdfFile);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.setDataAndType(path, "application/pdf");

                startActivity(intent);

However when I run it and press the button it says "The document cannot be opened because its is not a valid PDF document". This is driving me mad. Am I accessing the file correctly? Any ideas? Thanks

DMC
  • 1,184
  • 5
  • 21
  • 50
  • Duplicate of question found here: http://stackoverflow.com/questions/6491210/how-to-open-a-pdf-stored-either-in-res-raw-or-assets-folder – nu everest Feb 18 '15 at 21:55

2 Answers2

6

You have to copy the pdf from assets folder to sdcard folder.

.....
copyFile(this.getAssets().open("userguide.pdf"), new FileOutputStream(new File(getFilesDir(), "yourPath/userguide.pdf")));

File pdfFile = new File(getFilesDir(), "yourPath/userguide.pdf"); Uri path = Uri.fromFile(pdfFile);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.setDataAndType(path, "application/pdf");

                    startActivity(intent);


}

private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
          out.write(buffer, 0, read);
        }
    }
Jc Miñarro
  • 1,391
  • 10
  • 18
-1

You can insert your pdf in the folder assets in android and then try with:

File pdfFile = new File(getAsset().open("userguide.pdf"));
                Uri path = Uri.fromFile(pdfFile);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.setDataAndType(path, "application/pdf");

                startActivity(intent);

Edit: The URi from folder assets is: file:///android_asset/RELATIVE_PATH Then the source will be:

File pdfFile = new File("file:///android_asset/userguide.pdf");
                    Uri path = Uri.fromFile(pdfFile);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.setDataAndType(path, "application/pdf");

                    startActivity(intent);
Jc Miñarro
  • 1,391
  • 10
  • 18
  • 1
    File pdfFile = new File(getAsset().open("userguide.pdf"); doesn't compile.Its says The constructor File(InputStream) is undefined. – DMC Nov 22 '12 at 17:38
  • Now its saying the document path is not valid when I try and open it! – DMC Nov 22 '12 at 17:43
  • 1
    Have you inserted the pdf in your assets folder?? BTW it is no possible, because the pdf is inside of your sandbox and only your app is able to access to this path. – Jc Miñarro Nov 22 '12 at 17:49
  • Yeah its definitely in the assets folder! – DMC Nov 22 '12 at 17:52
  • I have added another answer with the solution. Maybe it has some mistake because I have written it in the webBrowser, I don't have a IDE like eclipse in this PC. But with it you can have an idea how to solve it. – Jc Miñarro Nov 22 '12 at 17:57
  • @JcMiñarro Even your edited answer is not working. Because other applications can't access the files in your assets . – Bharat Dodeja Aug 19 '16 at 17:57