java - Rendering 2 + pdf files -
i learned how render pdf file application. when tried 1 pdf worked perfectly, when try 2, automatically picks last asset (pdf) made.
the code in mainactivity:
import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.view.view; import android.widget.linearlayout; public class mainactivity extends appcompatactivity { private view btnrender; private linearlayout container; private view btnrendered; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btnrender = (view)findviewbyid(r.id.btn_render); container = (linearlayout)findviewbyid(r.id.fragment_layout); btnrendered = (view) findviewbyid(r.id.btn_rendered); //set event handling button btnrender.setonclicklistener(onclicklistener()); } private view.onclicklistener onclicklistener() { return new view.onclicklistener() { @override public void onclick(view v) { switch(v.getid()){ case r.id.btn_render: //replace fragment when clicked fragmentmanager fm = getsupportfragmentmanager(); fragmenttransaction ft = fm.begintransaction(); ft.replace(r.id.fragment_layout, new pdfrenderfragment()); ft.commit(); //gone button after btnrender.setvisibility(view.gone); container.setvisibility(view.visible); break; case r.id.btn_rendered: fragmentmanager fz = getsupportfragmentmanager(); fragmenttransaction fy = fz.begintransaction(); fy.replace(r.id.fragment_layout, new pdfrenderagment()); fy.commit(); //gone button after btnrender.setvisibility(view.gone); container.setvisibility(view.visible); break; } }; }; }}
first pdf:
import android.app.fragment; import android.app.activity; import android.graphics.bitmap; import android.graphics.pdf.pdfrenderer; import android.os.bundle; import android.os.parcelfiledescriptor; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.imageview; import java.io.ioexception; public class pdfrenderfragment extends android.support.v4.app.fragment { private parcelfiledescriptor filedescriptor; private pdfrenderer pdfrenderer; private pdfrenderer.page currentpage; private imageview image; private button btnprevious; private button btnnext; public pdfrenderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_pdfrender, container, false); } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); // retain view references. image = (imageview) view.findviewbyid(r.id.image); btnprevious = (button) view.findviewbyid(r.id.btn_previous); btnnext = (button) view.findviewbyid(r.id.btn_next); //set buttons event btnprevious.setonclicklistener(onactionlistener(-1)); //previous button clicked btnnext.setonclicklistener(onactionlistener(1)); //next button clicked int index = 0; // if there savedinstancestate (screen orientations, etc.), restore page index. if (null != savedinstancestate) { index = savedinstancestate.getint("current_page", 0); } showpage(index); } @override public void onattach(activity activity) { super.onattach(activity); try { openrenderer(activity); } catch (ioexception e) { e.printstacktrace(); log.i("fragment", "error occurred!"); log.e("fragment", e.getmessage()); activity.finish(); } } @override public void ondestroy() { try { closerenderer(); } catch (ioexception e) { e.printstacktrace(); } super.ondestroy(); } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); if (null != currentpage) { outstate.putint("current_page", currentpage.getindex()); } } /** * create pdf renderer * @param activity * @throws ioexception */ private void openrenderer(activity activity) throws ioexception { // reading pdf file assets directory. filedescriptor = activity.getassets().openfd("biology_in_c.pdf").getparcelfiledescriptor(); // pdfrenderer use render pdf. pdfrenderer = new pdfrenderer(filedescriptor); } /** * closes pdfrenderer , related resources. */ private void closerenderer() throws ioexception { if (null != currentpage) { currentpage.close(); } pdfrenderer.close(); filedescriptor.close(); } /** * shows specified page of pdf file screen * @param index page index. */ private void showpage(int index) { if (pdfrenderer.getpagecount() <= index) { return; } // make sure close current page before opening one. if (null != currentpage) { currentpage.close(); } //open specific page in pdf file currentpage = pdfrenderer.openpage(index); // important: destination bitmap must argb (not rgb). bitmap bitmap = bitmap.createbitmap(currentpage.getwidth(), currentpage.getheight(), bitmap.config.argb_8888); // here, render page onto bitmap. currentpage.render(bitmap, null, null, pdfrenderer.page.render_mode_for_display); // showing bitmap imageview image.setimagebitmap(bitmap); updateuidata(); } /** * updates state of 2 control buttons in response current page index. */ private void updateuidata() { int index = currentpage.getindex(); int pagecount = pdfrenderer.getpagecount(); btnprevious.setenabled(0 != index); btnnext.setenabled(index + 1 < pagecount); getactivity().settitle(getstring(r.string.app_name , index + 1, pagecount)); } private view.onclicklistener onactionlistener(final int i) { return new view.onclicklistener() { @override public void onclick(view v) { if (i < 0) {//go previous page showpage(currentpage.getindex() - 1); } else { showpage(currentpage.getindex() + 1); } } }; } }
second pdf:
import android.app.fragment; import android.app.activity; import android.graphics.bitmap; import android.graphics.pdf.pdfrenderer; import android.os.bundle; import android.os.parcelfiledescriptor; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.imageview; import java.io.ioexception; public class pdfrenderagment extends android.support.v4.app.fragment { private parcelfiledescriptor filedescriptor; private pdfrenderer pdfrenderer; private pdfrenderer.page currentpage; private imageview image; private button btnprevious; private button btnnext; public pdfrenderagment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_pdfrender, container, false); } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); // retain view references. image = (imageview) view.findviewbyid(r.id.image); btnprevious = (button) view.findviewbyid(r.id.btn_previous); btnnext = (button) view.findviewbyid(r.id.btn_next); //set buttons event btnprevious.setonclicklistener(onactionlistener(-1)); //previous button clicked btnnext.setonclicklistener(onactionlistener(1)); //next button clicked int index = 0; // if there savedinstancestate (screen orientations, etc.), restore page index. if (null != savedinstancestate) { index = savedinstancestate.getint("current_page", 0); } showpage(index); } @override public void onattach(activity activity) { super.onattach(activity); try { openrenderer(activity); } catch (ioexception e) { e.printstacktrace(); log.i("fragment", "error occurred!"); log.e("fragment", e.getmessage()); activity.finish(); } } @override public void ondestroy() { try { closerenderer(); } catch (ioexception e) { e.printstacktrace(); } super.ondestroy(); } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); if (null != currentpage) { outstate.putint("current_page", currentpage.getindex()); } } /** * create pdf renderer * @param activity * @throws ioexception */ private void openrenderer(activity activity) throws ioexception { // reading pdf file assets directory. filedescriptor = activity.getassets().openfd("biology_in_f.pdf").getparcelfiledescriptor(); // pdfrenderer use render pdf. pdfrenderer = new pdfrenderer(filedescriptor); } /** * closes pdfrenderer , related resources. */ private void closerenderer() throws ioexception { if (null != currentpage) { currentpage.close(); } pdfrenderer.close(); filedescriptor.close(); } /** * shows specified page of pdf file screen * @param index page index. */ private void showpage(int index) { if (pdfrenderer.getpagecount() <= index) { return; } // make sure close current page before opening one. if (null != currentpage) { currentpage.close(); } //open specific page in pdf file currentpage = pdfrenderer.openpage(index); // important: destination bitmap must argb (not rgb). bitmap bitmap = bitmap.createbitmap(currentpage.getwidth(), currentpage.getheight(), bitmap.config.argb_8888); // here, render page onto bitmap. currentpage.render(bitmap, null, null, pdfrenderer.page.render_mode_for_display); // showing bitmap imageview image.setimagebitmap(bitmap); updateuidata(); } /** * updates state of 2 control buttons in response current page index. */ private void updateuidata() { int index = currentpage.getindex(); int pagecount = pdfrenderer.getpagecount(); btnprevious.setenabled(0 != index); btnnext.setenabled(index + 1 < pagecount); getactivity().settitle(getstring(r.string.app_name , index + 1, pagecount)); } private view.onclicklistener onactionlistener(final int i) { return new view.onclicklistener() { @override public void onclick(view v) { if (i < 0) {//go previous page showpage(currentpage.getindex() - 1); } else { showpage(currentpage.getindex() + 1); } } }; } }
there no syntax errors, lead me believe there error in 'logic' coding. if answer this, grateful!
Comments
Post a Comment