android - Not showing the first page when started -
good day, i'm new pdf renderer on android studio > api 21, found simple code read pdf on assets folder. problem when it's load first page of file not showing unless press next button. application aims read user's manual pdf.
package com.bloxofcode.phase1app; import android.app.alertdialog; import android.content.context; import android.content.res.assetmanager; import android.graphics.bitmap; import android.graphics.matrix; import android.graphics.rect; import android.graphics.pdf.pdfrenderer; import android.os.parcelfiledescriptor; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.imageview; import java.io.file; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; public class usersmanual extends appcompatactivity { private imageview imageview; private int currentpage = 0; private button next, previous; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_users_manual); next = (button) findviewbyid(r.id.next); previous = (button) findviewbyid(r.id.previous); render(); next.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { currentpage++; render(); } }); previous.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { currentpage--; render(); } }); } private void render() { try{ imageview = (imageview) findviewbyid(r.id.image); int req_width = imageview.getwidth(); int req_height = imageview.getheight(); bitmap bitmap = bitmap.createbitmap(req_width, req_height, bitmap.config.argb_8888); //file file = new file("/sdcard/download/sample.pdf"); assetmanager assetmanager = getassets(); inputstream in = null; outputstream out = null; file file = new file(getfilesdir(), "learning python 5th ed 2013.pdf"); try { in = assetmanager.open("learning python 5th ed 2013.pdf"); out = openfileoutput(file.getname(), context.mode_world_readable); copyfile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } catch (exception e) { log.e("tag", e.getmessage()); } //parcelfiledescriptor p = getapplicationcontext().getassets().openfd("learning python 5th ed 2013.pdf").getparcelfiledescriptor(); file f = new file(getfilesdir() + "/learning python 5th ed 2013.pdf"); pdfrenderer renderer = new pdfrenderer(parcelfiledescriptor.open(f, parcelfiledescriptor.mode_read_only)); if(currentpage <= 0) { currentpage = 0; } else if(currentpage > renderer.getpagecount()) { currentpage = renderer.getpagecount() - 1; } matrix m = imageview.getimagematrix(); rect rect = new rect(0, 0, req_width, req_height); renderer.openpage(currentpage).render(bitmap,rect, null, pdfrenderer.page.render_mode_for_display); imageview.setimagematrix(m); imageview.setimagebitmap(bitmap); imageview.invalidate(); } catch(exception e) { e.printstacktrace(); } } 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); } } }
req_width , req_height 0. try adding values them relating screen size
Comments
Post a Comment