0

I want to render a pdf which in the raw folder with ParcelFileDescriptor tried too many methods from the other posts but none of them worked for me. Below is the code

I copied the code from the post you suggested and modified my code but still pdf is not opening even its showing no error.

 public void render()
    {
        try
        {
            imgv = (ImageView) findViewById(R.id.img);
            int w = imgv.getWidth();
            int h = imgv.getHeight();
            Bitmap bm = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_4444);
            File fileBrochure = new File(Environment.getExternalStorageDirectory() + "/" + "abcd.pdf");
            if (!fileBrochure.exists())
            {
                CopyAssetsbrochure();
            }

            /** PDF reader code */
            File file = new File(Environment.getExternalStorageDirectory() + "/" + "abcd.pdf");
           // File file = new File("android.resource://com.nyt.ilm.mytestpdfreader/raw/abcd.pdf");
            PdfRenderer render = new PdfRenderer(ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY));
            if (CurrentPage < 0)
            { CurrentPage =0;
            }
           else if (CurrentPage > render.getPageCount()){
          CurrentPage = render.getPageCount() - 1;
        }
            Matrix m = imgv.getImageMatrix();
            Rect rect = new Rect(0,0,w,h);
            render.openPage(CurrentPage).render(bm,rect,m,PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
            imgv.setImageMatrix(m);
            imgv.setImageBitmap(bm);
            imgv.invalidate();
        }
         catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    //method to write the PDFs file to sd card
    private void CopyAssetsbrochure() {
        AssetManager assetManager = getAssets();
        String[] files = null;
        try
        {
            files = assetManager.list("");
        }
        catch (IOException e)
        {
            Log.e("tag", e.getMessage());
        }
        for(int i=0; i<files.length; i++)
        {
            String fStr = files[i];
            if(fStr.equalsIgnoreCase("abcd.pdf"))
            {
                InputStream in = null;
                OutputStream out = null;
                try
                {
                    in = assetManager.open(files[i]);
                    out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + files[i]);
                    copyFile(in, out);
                    in.close();
                    in = null;
                    out.flush();
                    out.close();
                    out = null;
                    break;
                }
                catch(Exception e)
                {
                    Log.e("tag", e.getMessage());
                }
            }
        }
    }

    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);
        }
    }
Community
  • 1
  • 1
  • Thanks Surya, I read the post but I did not understand what to do? may be I am a bit tired and sleepy. I just need solution for this line File file = new File("android.resource://com.nyt.ilm.mytestpdfreader/raw/abcd.pdf"); – Naveed Alam Tareen Dec 14 '16 at 06:39
  • I tried this already but its not working as well. – Naveed Alam Tareen Dec 15 '16 at 14:45
  • fwiw the following has details of some of the issues I had with using `PdfRenderer`...hopefully it will help http://stackoverflow.com/questions/41112281/android-v23-pdfreader-keep-crashing/41409103#41409103 – John O'Reilly Jan 04 '17 at 14:40

0 Answers0