Display PDF in assets folder (inside APK)

Display PDF in assets folder (inside APK) - Hallo sahabat Android News, Pada Artikel yang anda baca kali ini dengan judul Display PDF in assets folder (inside APK), kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel PDF, Artikel PdfRenderer, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Display PDF in assets folder (inside APK)
link : Display PDF in assets folder (inside APK)

Baca juga


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.



Demikianlah Artikel Display PDF in assets folder (inside APK)

Sekianlah artikel Display PDF in assets folder (inside APK) kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Display PDF in assets folder (inside APK) dengan alamat link https://androidthenews.blogspot.com/2017/02/display-pdf-in-assets-folder-inside-apk.html

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

Posting Komentar