java - How to select a PDF file using startActivityForResult intent and return result in a second class -


i relatively new android programming, want create pdf reader calls inbuilt file manager select pdf file , display pdf file in second class. here code far still having issues, think second activity unable selected pdf file , unable fix error

     mainactivity       package com.example.user.projectapp;       import android.app.activity;      import android.app.listactivity;      import android.content.context;      import android.content.intent;      import android.database.cursor;      import android.net.uri;      import android.provider.openablecolumns;      import android.support.v7.app.appcompatactivity;      import android.os.bundle;      import android.util.log;      import android.view.view;      import android.widget.arrayadapter;      import android.widget.listview;       import java.io.file;      import java.io.ioexception;      import java.io.inputstream;      import java.net.uri;      import java.net.urisyntaxexception;      import java.util.arraylist;      import java.util.collections;      import java.util.list;       public class mainactivity extends appcompatactivity {       public static string path;      private static final int pickfile_request_code = 0;      file tempfile;       @override      protected void oncreate(bundle savedinstancestate) {      super.oncreate(savedinstancestate);      setcontentview(r.layout.activity_main);      getfile();      }        public void getfile() {        intent intent = new intent(intent.action_get_content);       intent.settype("application/pdf");       path = intent.getstringextra("path");       intent.putextra("path",path);       startactivityforresult(intent, pickfile_request_code);       }        second activity         package com.example.user.projectapp;        import android.annotation.targetapi;        import android.content.intent;       import android.graphics.bitmap;       import android.graphics.pdf.pdfrenderer;       import android.os.build;       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 java.io.file;         public class main2activity extends appcompatactivity {        int pagecount = -1;       button buttonnext, buttonprevious;       imageview imageview;        @override       protected void oncreate(bundle savedinstancestate) {       super.oncreate(savedinstancestate);       setcontentview(r.layout.activity_main2);        buttonnext = (button) findviewbyid(r.id.button1);       buttonprevious = (button) findviewbyid(r.id.button2);       imageview = (imageview) findviewbyid(r.id.imageview);         buttonnext.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             pagecount++;              render();          }        });      buttonprevious.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             pagecount--;              render();         }        });         }         @targetapi(build.version_codes.lollipop)        public void render() {         try {         int width = imageview.getwidth();         int height = imageview.getheight();         bitmap bitmap = bitmap.createbitmap(width, height,                bitmap.config.argb_4444);          intent intent = getintent();         string data = intent.getstringextra("path");         file file = new file(data);          pdfrenderer renderer = new              pdfrenderer(parcelfiledescriptor.open(file, parcelfiledescriptor.mode_read_only));          if (pagecount < 0) {             pagecount = 0;         } else if (pagecount > renderer.getpagecount()) {             pagecount = renderer.getpagecount() - 1;         }         renderer.openpage(pagecount).render(bitmap, null, null, pdfrenderer.page.render_mode_for_display);         imageview.setimagebitmap(bitmap);         imageview.invalidate();      } catch (exception e) {         e.printstacktrace();     }     }      @override     public void onbackpressed() {     super.onbackpressed();     intent intent = new intent(this, mainactivity.class);     startactivity(intent);      }     } 

and logcat gives

    03-28 17:19:33.387 2492-2492/com.example.user.projectapp w/system.err: java.lang.nullpointerexception: attempt invoke virtual method 'char[] java.lang.string.tochararray()' on null object reference     03-28 17:19:33.400 2492-2492/com.example.user.projectapp w/system.err:     @ java.io.file.fixslashes(file.java:185)     03-28 17:19:33.400 2492-2492/com.example.user.projectapp w/system.err:     @ java.io.file.<init>(file.java:134)     03-28 17:19:33.401 2492-2492/com.example.user.projectapp w/system.err:     @ com.example.user.projectapp.main2activity.render(main2activity.java:69)     03-28 17:19:33.401 2492-2492/com.example.user.projectapp w/system.err:     @ com.example.user.projectapp.main2activity$1.onclick(main2activity.java:42)     03-28 17:19:33.401 2492-2492/com.example.user.projectapp w/system.err:     @ android.view.view.performclick(view.java:4764)     03-28 17:19:33.401 2492-2492/com.example.user.projectapp w/system.err:     @ android.view.view$performclick.run(view.java:19844)     03-28 17:19:33.401 2492-2492/com.example.user.projectapp w/system.err:     @ android.os.handler.handlecallback(handler.java:739)     03-28 17:19:33.401 2492-2492/com.example.user.projectapp w/system.err:     @ android.os.handler.dispatchmessage(handler.java:95)     03-28 17:19:33.401 2492-2492/com.example.user.projectapp w/system.err:     @ android.os.looper.loop(looper.java:135)     03-28 17:19:33.401 2492-2492/com.example.user.projectapp w/system.err:     @ android.app.activitythread.main(activitythread.java:5351)     03-28 17:19:33.401 2492-2492/com.example.user.projectapp w/system.err:     @ java.lang.reflect.method.invoke(native method)     03-28 17:19:33.401 2492-2492/com.example.user.projectapp w/system.err:     @ java.lang.reflect.method.invoke(method.java:372)     03-28 17:19:33.401 2492-2492/com.example.user.projectapp w/system.err:     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:908)     03-28 17:19:33.401 2492-2492/com.example.user.projectapp w/system.err:     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:703) 

code modified based on 1 activity only...... yet onactivityresult not return result,

file myfile; public static string result; public string path; private static final int pickfile_request_code = 12; int pagecount = -1; button buttonnext, buttonprevious; imageview imageview; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     getfile();      buttonnext = (button) findviewbyid(r.id.button1);     buttonprevious = (button) findviewbyid(r.id.button2);     imageview = (imageview) findviewbyid(r.id.imageview);       buttonnext.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             pagecount++;              render();          }     });     buttonprevious.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             pagecount--;              render();         }     });  } public void getfile() {      intent intent = new intent(intent.action_get_content);     intent.settype("application/pdf");     //intent.settype("image/");     // path = intent.getstringextra("path");      //intent.putextra("path",path);     //intent.addcategory(intent.category_openable);     startactivityforresult(intent, 12); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) {     super.onactivityresult(requestcode, resultcode, data);     if (requestcode==12){         if (data != null){             toast.maketext(this,"file selected",toast.length_long).show();           // result=data.getstringextra("data");             uri uri=data.getdata();           //  string uristring = uri.tostring();              myfile = new file(uri.tostring());             path = myfile.getabsolutepath();            // result=data.tostring();            // uri uri=data.getparcelableextra(path);            // path=fileuri.tostring();             this.render();         }      }  } @targetapi(build.version_codes.lollipop) public void render() {      try {         int width = imageview.getwidth();         int height = imageview.getheight();         bitmap bitmap = bitmap.createbitmap(width, height, bitmap.config.argb_4444);          //intent intent = getintent();         //string path = intent.getstringextra("path");          // uri uri = uri.parse(uri_str);          file file = new file(path);         //file storage = environment.getexternalstoragedirectory();         // intent.setflags(intent.flag_activity_reorder_to_front);           pdfrenderer renderer = new pdfrenderer(parcelfiledescriptor.open(file, parcelfiledescriptor.mode_read_only));          if (pagecount < 0) {             pagecount = 0;         } else if (pagecount > renderer.getpagecount()) {             pagecount = renderer.getpagecount() - 1;         }          // matrix m = imageview.getimagematrix();         // rect rect=new rect(0,0,width,height);         renderer.openpage(pagecount).render(bitmap, null, null, pdfrenderer.page.render_mode_for_display);         //imageview.setimagematrix(m);         imageview.setimagebitmap(bitmap);         imageview.invalidate();      } catch (exception e) {         e.printstacktrace();     } } } 

and logcat error...

    03-29 11:32:47.276 21674-21674/com.example.user.newtry w/system.err: java.io.filenotfoundexception: no such file or directory     03-29 11:32:47.283 21674-21674/com.example.user.newtry w/system.err:     @ android.os.parcel.openfiledescriptor(native method)     03-29 11:32:47.283 21674-21674/com.example.user.newtry w/system.err:     @ android.os.parcelfiledescriptor.openinternal(parcelfiledescriptor.java:253)     03-29 11:32:47.283 21674-21674/com.example.user.newtry w/system.err:     @      android.os.parcelfiledescriptor.open(parcelfiledescriptor.java:199) 

yes.. because path null.. reason getting null pointer exception


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -