-1

How can i open a PDFs file from raw folder? This code for access PDFs file from SD card..

File file = new File(getFilesDir(), "teen_ebook.pdf");

  try {

        if (file.exists()) {

            Uri path = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            Log.e("IR", "No exception");

        }
        else {
            Toast.makeText(PdfActivity.this, "File NotFound",Toast.LENGTH_SHORT).show();
        }
    }
    catch (ActivityNotFoundException e) {

    Toast.makeText(PdfActivity.this, "No Application Available to View PDF", Toast.LENGTH_SHORT).show();
    }

But i want to access PDFs file from raw folder? Any one here. Please suggests me.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
Amit D
  • 11
  • 1
  • 4

1 Answers1

1

You can not directly open PDF file from the raw folder. It requires PDFViewer application to open PDF file.

You will have to copy your PDF file into sdcard and then you can open to read it.

public void openPDF(){

  copyFile(getResources().openRawResource(R.raw.hello_world);
       , new FileOutputStream(new File(getFilesDir(), "yourPath/teen_ebook.pdf")));

        File pdfFile = new File(getFilesDir(), "yourPath/teen_ebook.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);
        }
    }
GrIsHu
  • 29,068
  • 10
  • 64
  • 102