0

Anyone can help in this code, the pdf file is not loading in app and just showing blank white screen, Logcat showing FileNotFoundExeeption: /storage/sdcard/raw/ourpdf.pdf.

i am trying to make an app that will show information while i click buttons and every button will be active for specific pdf file reading. Any specific help please.

Thanks for help

part1

package com.code.androidpdf;

public class MainActivity extends Activity {

//Globals:
private WebView wv;
private int ViewSize = 0;

//OnCreate Method:
@Override
protected void onCreate(Bundle savedInstanceState)

{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Settings
    PDFImage.sShowImages = true; // show images
    PDFPaint.s_doAntiAlias = true; // make text smooth
    HardReference.sKeepCaches = true; // save images in cache

    //Setup above
    wv = (WebView)findViewById(R.id.webView1);
    wv.getSettings().setBuiltInZoomControls(true);//show zoom buttons
    wv.getSettings().setSupportZoom(true);//allow zoom
    //get the width of the webview
    wv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            ViewSize = wv.getWidth();
            wv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });

    pdfLoadImages();//load images

}

private void pdfLoadImages() {
    try
    {
        // run async
        new AsyncTask<Void, Void, Void>()
                {
                    // create and show a progress dialog
                    ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "", "Opening...");

                    @Override
                    protected void onPostExecute(Void result)
                    {
                        //after async close progress dialog
                        progressDialog.dismiss();
                    }

                    @Override
                    protected Void doInBackground(Void... params)
                    {
                        try
                        {
                            // select a document and get bytes
    File file = new File(Environment.getExternalStorageDirectory().getPath()+"/randompdf.pdf");
                            RandomAccessFile raf = new RandomAccessFile(file, "r");
                            FileChannel channel = raf.getChannel();
                            net.sf.andpdf.nio.ByteBuffer bb = null ;
                            raf.close();
                            // create a pdf doc
                            PDFFile pdf = new PDFFile(bb);
                            //Get the first page from the pdf doc
                            PDFPage PDFpage = pdf.getPage(1, true);
                            //create a scaling value according to the WebView Width
                            final float scale = ViewSize / PDFpage.getWidth() * 0.95f;
                            //convert the page into a bitmap with a scaling value
                            Bitmap page = PDFpage.getImage((int)(PDFpage.getWidth() * scale), (int)(PDFpage.getHeight() * scale), null, true, true);
                            //save the bitmap to a byte array
                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                            page.compress(Bitmap.CompressFormat.PNG, 100, stream);
                            stream.close();
                            byte[] byteArray = stream.toByteArray();
                            //convert the byte array to a base64 string
                            String base64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
                            //create the html + add the first image to the html
                            String html = "<!DOCTYPE html><html><body bgcolor=\"#7f7f7f\"><img src=\"data:image/png;base64,"+base64+"\" hspace=10 vspace=10><br>";
                            //loop through the rest of the pages and repeat the above
                            for(int i = 2; i <= pdf.getNumPages(); i++)
                            {
                                PDFpage = pdf.getPage(i, true);
                                page = PDFpage.getImage((int)(PDFpage.getWidth() * scale), (int)(PDFpage.getHeight() * scale), null, true, true);
                                stream = new ByteArrayOutputStream();
                                page.compress(Bitmap.CompressFormat.PNG, 100, stream);
                                stream.close();
                                byteArray = stream.toByteArray();
                                base64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
                                html += "<img src=\"data:image/png;base64,"+base64+"\" hspace=10 vspace=10><br>";
                            }
                            html += "</body></html>";
                            //load the html in the webview
                            wv.loadDataWithBaseURL("", html, "text/html","UTF-8", "");
                    }
                    catch (Exception e)
                    {
                        Log.d("CounterA", e.toString());
                    }
                        return null;
                    }
                }.execute();
                System.gc();// run GC
    }
    catch (Exception e)
    {
        Log.d("error", e.toString());
    }
}

}
Nayeem Hyder Riddhi
  • 561
  • 1
  • 6
  • 25

2 Answers2

0

You can get an InputStream for the file using

getResources().openRawResource(R.raw.ourpdf)

Docs: http://developer.android.com/reference/android/content/res/Resources.html#openRawResource(int)

Shlomo Zalman Heigh
  • 3,968
  • 8
  • 41
  • 71
0

It is (sadly) not possible to view a PDF that is stored locally in your devices. Android L has introduced the feature. So, to display a PDF , you have two options:

  1. See this answer for using webview How to open local pdf file in webview in android? (note that this requires an internet connection)
  2. Use a third party pdf Viewer.

You can also send an intent for other apps to handle your pdf.

Community
  • 1
  • 1
harveyslash
  • 5,906
  • 12
  • 58
  • 111