How can i open pdf file from sdcard which i can from resource folder

I am using the pdf viewer library and can show the pdf from the assets folder, but while I am trying to do it from the sdcard, it just shows the progrss bar for a long time.

I am using below code to show from Asset

    AssetManager assetManager = getAssets();
    InputStream in = null;
    OutputStream out = null;
    File file = new File(getFilesDir(), "sample.pdf");
    try {
        in = assetManager.open("sample.pdf");
        out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        Log.e("tag", e.getMessage());
    }

    Intent intent = new Intent(this, MyPdfViewerActivity.class);
    intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, getFilesDir() + "/sample.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);
    }
}}

      

And I am using below code to show with Sdcard

    Intent intent = new Intent(this, MyPdfViewerActivity.class);
    intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,
            "mnt/sdcard/sample.pdf");

/*  intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,
            "assets/sample.pdf");*/

    startActivity(intent);
}

      

+3


source to share


1 answer


You can try to access the PDF file from SDCard like this:

File myPdfFile = new File(Environment.getExternalStorageDirectory(),"sample.pdf");
String path = myPdfFile.getAbsolutePath();

//Open the Pdf with this Method            
openPdfFileWithFilePath(path);

protected void openPdfFileWithFilePath(String path) {
// TODO Auto-generated method stub
 try {
        final Intent intent = new Intent(this, MyPdfViewerActivity.class);
        intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,path);
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

      



Hope this helps.

0


source







All Articles