Display Pdf Inwards Assets Folder (Inside Apk)

Posted on

The instance “Display PDF using PdfRenderer” demo how to display PDF stored inward sdcard, this instance demo how to display PDF stored inward assets within APK.

First, y’all accept to create assets folder in addition to re-create your PDF into it.

Then y’all accept to edit aaptOptions inward build.gradle (Module: app), non to orbit “pdf” file.

     aaptOptions {         noCompress "pdf"     } 

Layout file, cite to the example “Create PDF using PdfDocument“.


package com.blogspot.android_er.androidpdf;  import android.content.res.AssetFileDescriptor; import android.content.res.AssetManager; import android.graphics.Bitmap; import android.graphics.pdf.PdfRenderer; import android.os.Bundle; import android.os.ParcelFileDescriptor; import android.support.v7.app.AppCompatActivity; import android.widget.ImageView; import android.widget.Toast;  import java.io.IOException;  world shape MainActivity extends AppCompatActivity {      ImageView pdfView;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          pdfView = (ImageView)findViewById(R.id.pdfview);          endeavor {             openPDF();         } select grip of (IOException e) {             e.printStackTrace();             Toast.makeText(this,                     "Something Wrong: " + e.toString(),                     Toast.LENGTH_LONG).show();         }     }      private void openPDF() throws IOException {          //open file inward assets          AssetManager assetManager = getAssets();         AssetFileDescriptor assetFileDescriptor =                  assetManager.openFd("test.pdf");         ParcelFileDescriptor fileDescriptor =                  assetFileDescriptor.getParcelFileDescriptor();          //open file from sdcard         /*         String targetPdf = "/sdcard/test.pdf";         File file = novel File(targetPdf);          ParcelFileDescriptor fileDescriptor = null;         fileDescriptor = ParcelFileDescriptor.open(                 file, ParcelFileDescriptor.MODE_READ_ONLY);         */          //min. API Level 21         PdfRenderer pdfRenderer = null;         pdfRenderer = novel PdfRenderer(fileDescriptor);          terminal int pageCount = pdfRenderer.getPageCount();         Toast.makeText(this,                 "pageCount = " + pageCount,                 Toast.LENGTH_LONG).show();          //Display page 0         PdfRenderer.Page rendererPage = pdfRenderer.openPage(1);         int rendererPageWidth = rendererPage.getWidth();         int rendererPageHeight = rendererPage.getHeight();         Bitmap bitmap = Bitmap.createBitmap(                 rendererPageWidth,                 rendererPageHeight,                 Bitmap.Config.ARGB_8888);         rendererPage.render(bitmap, null, null,                 PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);          pdfView.setImageBitmap(bitmap);         rendererPage.close();          pdfRenderer.close();          assetFileDescriptor.close();     } }  

Remark: If y’all reported amongst the error “java.io.IOException: non create document. Error:”, read next post.