The PDF renderer class is not rendering a PDF on Android SDK 21 -


here's code i'm using render pdf called "answerkey.pdf" that's stored in "download/adobe reader/answerkey.pdf"

package com.practice.pdftest;  import android.app.activity; import android.content.context; import android.graphics.bitmap; import android.graphics.matrix; import android.graphics.rect; import android.graphics.pdf.pdfrenderer; import android.os.environment; import android.os.parcelfiledescriptor; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.imageview; import android.widget.textview; import android.widget.toast;  import java.io.file; import java.io.filenotfoundexception; import java.io.ioexception;  public class mainactivity extends activity {       private imageview imageview;     private button next, previous;     private textview tv;     private int currentpage = 0;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          next = (button)findviewbyid(r.id.next);         next.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 currentpage++;                 render();             }         });          previous = (button)findviewbyid(r.id.previous);         previous.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 currentpage--;                 render();             }         }); //        tv = (textview)findviewbyid(r.id.testtext); //        tv.settext(environment.getexternalstoragedirectory().getabsolutepath()); // //        toast toast = toast.maketext(getapplicationcontext(),environment.getexternalstoragedirectory().getabsolutepath(),toast.length_long); //        toast.show();     }      private void render() {         try {             imageview = (imageview)findviewbyid(r.id.imageview);              int req_width = 1;             int req_height = 1;              req_width = imageview.getwidth();             req_height = imageview.getheight();             bitmap bitmap = bitmap.createbitmap(req_width, req_height, bitmap.config.argb_4444);             file file = new file(environment.getdatadirectory().getpath()+"adobe reader/answerkey.pdf");             pdfrenderer pdfrenderer = new pdfrenderer(parcelfiledescriptor.open(file, parcelfiledescriptor.mode_read_only));             if(currentpage < 0) {                 currentpage=0;             }             else if (currentpage > pdfrenderer.getpagecount()) {                 currentpage = pdfrenderer.getpagecount()-1;             }              matrix matrix = imageview.getimagematrix();             rect rect = new rect(0,0,req_height,req_width);             pdfrenderer.openpage(currentpage).render(bitmap,rect,matrix,pdfrenderer.page.render_mode_for_display);             imageview.setimagematrix(matrix);             imageview.setimagebitmap(bitmap);             imageview.invalidate();           } catch (filenotfoundexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }     } } 

here's layout.xml file i've made -

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingbottom="@dimen/activity_vertical_margin"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:orientation="vertical"     tools:context="com.practice.pdftest.mainactivity">     <imageview        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/imageview"        android:layout_weight="4"        android:background="@android:color/white"        android:layout_marginbottom="20dp"        />     <!--<textview-->         <!--android:layout_width="wrap_content"-->         <!--android:layout_height="wrap_content"-->         <!--android:id="@+id/testtext"-->         <!--android:text="test"-->          <!--/>-->     <linearlayout         android:layout_width="match_parent"         android:orientation="horizontal"         android:layout_weight="2"         android:gravity="center"         android:layout_height="wrap_content"         android:layout_margintop="-100dp">          <button             android:id="@+id/previous"             android:layout_width="200dp"             android:layout_height="wrap_content"             android:text="previous"             />          <button             android:id="@+id/next"             android:layout_width="200dp"             android:layout_height="wrap_content"             android:text="next" />      </linearlayout> </linearlayout> 

for reason, pdf isn't being displayed on screen, keep getting white, blank background. doing wrong? path incorrect? have no sd card on device. or doing wrong bitmap?

is path incorrect?

yes.

i have no sd card on device

that fine. removable storage. interpreting "download/adobe reader/answerkey.pdf" referring on external storage.

the recommended way location replace:

file file = new file(environment.getdatadirectory().getpath()+"adobe reader/answerkey.pdf"); 

with:

file file = new file(environment.getexternalstoragepublicdirectory(environment.directory_downloads), "adobe reader/answerkey.pdf"); 

note app need request read_external_storage or write_external_storage permission, , involve runtime permissions on android 6.0+ (if targetsdkversion 23 or higher).


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -