Display PDF in assets folder (inside APK)

Display PDF in assets folder (inside APK) - Hello Friend GENERAL INFO FOR ANDROID DEVELOPMENT, In the article you read this time with the title Display PDF in assets folder (inside APK), We have prepared this article for you to read and retrieve information there in. Hopefully the contents of postings Artikel PDF, Artikel PdfRenderer, We write this you can understand. Alright, good read.

The Title : Display PDF in assets folder (inside APK)
link : Display PDF in assets folder (inside APK)

Read also


Display PDF in assets folder (inside APK)

The example "Display PDF using PdfRenderer" show how to display PDF stored in sdcard, this example show how to display PDF stored in assets inside APK.


First, you have to create assets folder and copy your PDF into it.


Then you have to edit aaptOptions in build.gradle (Module: app), not to compass "pdf" file.


aaptOptions {
noCompress "pdf"
}


Layout file, refer to the example "Create PDF using PdfDocument".

MainActivity.java
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;

public class 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);

try {
openPDF();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this,
"Something Wrong: " + e.toString(),
Toast.LENGTH_LONG).show();
}
}

private void openPDF() throws IOException {

//open file in 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 = new File(targetPdf);

ParcelFileDescriptor fileDescriptor = null;
fileDescriptor = ParcelFileDescriptor.open(
file, ParcelFileDescriptor.MODE_READ_ONLY);
*/

//min. API Level 21
PdfRenderer pdfRenderer = null;
pdfRenderer = new PdfRenderer(fileDescriptor);

final 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 you reported with the error "java.io.IOException: not create document. Error:", read next post.



Thus Article Display PDF in assets folder (inside APK)

The End Article Display PDF in assets folder (inside APK) This time, hopefully can give benefits to all of you. well, see you in posting other articles.

You are now reading the article Display PDF in assets folder (inside APK) with the link address http://xorislogo.blogspot.com/2017/02/display-pdf-in-assets-folder-inside-apk.html

0 Response to "Display PDF in assets folder (inside APK)"

Post a Comment