0

I have a PDF file stored in my assets. I want to load the PDF from my assets and read it in the app itself without using any 3rd party app to view.

I got the solution in this link. It works fine when selecting files from sdcard.

Community
  • 1
  • 1
Goofy
  • 6,098
  • 17
  • 90
  • 156

2 Answers2

2

Following snippet might help you accessing files from asset folder and then open it:

private void ReadFromAssets()
{
    AssetManager assetManager = getAssets();

    InputStream in = null;
    OutputStream out = null;
    File file = new File(getFilesDir(), "file.pdf");
    try
    {
        in = assetManager.open("file.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(Intent.ACTION_VIEW);
    intent.setDataAndType(
            Uri.parse("file://" + getFilesDir() + "/file.pdf"),
            "application/pdf");

    startActivity(intent);
}

and copyFile method is as follows:

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);
    }
}

EDIT

For that purpose you'll have to use an ecternal library. It's explained quite well in the link below: Render a PDF file using Java on Android

Hope this will help you.

Community
  • 1
  • 1
Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80
  • yes yes i had seen that i am using PDFViewer lib but thys have not explained how toimplement, can you please help me on that – Goofy Feb 12 '13 at 11:21
  • project on GitHib explains this. If you have not found the link on the mentioned link, use thsi direct link. https://github.com/jblough/Android-Pdf-Viewer-Library – Usama Sarwar Feb 12 '13 at 11:23
  • No i meant to say that i am able to display the pdf from sdcard using PDFViewer lib but using the same lib to how read files from assests? – Goofy Feb 12 '13 at 11:31
  • have a look at the ans I gave you earlier...there file is being brought from assets... – Usama Sarwar Feb 12 '13 at 11:48
  • carry on to Library part when it comes to opening it. – Usama Sarwar Feb 12 '13 at 12:10
  • yes i have done that also but its not showing the data properly, it shows blank screen – Goofy Feb 12 '13 at 12:13
  • Context.MODE_WORLD_READABLE is deprecated and not a good approach. – Warpzit Oct 07 '15 at 13:17
1

Its better if you can open it using a webview

WebView web = (WebView) findViewById(R.id.webView1);

web.loadUrl("file:///android_asset/yourpdf.pdf");

Hope it works.

Ooops just now I checked, the pdf cannot be loaded in the web view Sorry

Amalan Dhananjayan
  • 2,277
  • 1
  • 34
  • 47
  • 1
    No buddy its an expensive approach and also it takes some time to load which the user cant wait – Goofy Feb 12 '13 at 11:23