java - Get path to image chosen from gallery(on SD card) -


i cannot path image chosen gallery thats on sd card, there no problem images stored on device. there dozens of similar or duplicate questions this, none of them working.

the recommended approach (by @commonsware) use contentresolver , call openinputstream(), tried reading bytes openinputstream() creating temp file , using path, no luck. "bytes read: " system.err.println call shows 0 bytes read.

i tried this, this, this, this, this, , this, none work. lot of these answers lead calling bitmapfactory#decodestream(), problem is, couldn't care less displaying image, need path!!.

please help!

code far:

 @override public void onactivityresult(int requestcode, int resultcode, intent data) {      if(resultcode == activity.result_ok){          switch(requestcode){              case select_from_gallery:                 fromgallery = true;                 path = getrealpathfromuri(data.getdata());                 system.err.println("*******path: " + path + "*********");                  break;          }      }  }  public string getrealpathfromuri(uri contenturi) {     string thepath = null;     string[] filepathcolumn = {mediastore.images.media.data};     cursor cursor = getcontext().getcontentresolver().query(contenturi, filepathcolumn, null, null, null);     if(cursor.movetofirst()){         int columnindex = cursor.getcolumnindex(filepathcolumn[0]);         thepath = cursor.getstring(columnindex);     }     cursor.close();      if(thepath == null){         return getimagepathexternal(contenturi);     }else {         return thepath;     } }  public string getimagepathexternal(uri uri){     cursor cursor = getcontext().getcontentresolver().query(uri, null, null, null, null);     cursor.movetofirst();     string document_id = cursor.getstring(0);     document_id = document_id.substring(document_id.lastindexof(":")+1);     cursor.close();      cursor = getcontext().getcontentresolver().query(             android.provider.mediastore.images.media.external_content_uri,             null, mediastore.images.media._id + " = ? ", new string[]{document_id}, null);     cursor.movetofirst();     string path = cursor.getstring(cursor.getcolumnindex(mediastore.images.media.data));     cursor.close();      if(path == null){          system.err.println("****************trying bytes route******************");         try{              inputstream in = getcontext().getcontentresolver().openinputstream(uri);              byte[] resultbuff = new byte[0];             byte[] buff = new byte[1024];             int k ;             while((k = in.read(buff, 0, buff.length)) > -1) {                 byte[] tbuff = new byte[resultbuff.length + k]; // temp buffer size = bytes read + bytes last read                 system.arraycopy(resultbuff, 0, tbuff, 0, resultbuff.length); // copy previous bytes                 system.arraycopy(buff, 0, tbuff, resultbuff.length, k);  // copy current lot                 resultbuff = tbuff; // call temp buffer result buff             }              system.err.println("****************bytes read: "+resultbuff.length+" ******************");              file tem = new file(_tempimagedir, "temp.jpg");              fileoutputstream out = new fileoutputstream(tem);              out.write(resultbuff, 0, resultbuff.length);              return tem.getabsolutepath();          }catch(exception e){             e.printstacktrace();             return null;         }      }      return path; } 

logcat(this exception never changes, no matter approach use):

e/androidruntime: fatal exception: main                                                                   process: com.venon.nakomangsp, pid: 20610                                                                   java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=132074, result=-1, data=intent { dat=content://media/external/images/media/4147 (has extras) }} activity {com.venon.nakomangsp/com.venon.nakomangsp.signupactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'int android.graphics.bitmap.getwidth()' on null object reference                                                                       @ android.app.activitythread.deliverresults(activitythread.java:4058)                                                                       @ android.app.activitythread.handlesendresult(activitythread.java:4101)                                                                       @ android.app.activitythread.access$1400(activitythread.java:177)                                                                       @ android.app.activitythread$h.handlemessage(activitythread.java:1497)                                                                       @ android.os.handler.dispatchmessage(handler.java:102)                                                                       @ android.os.looper.loop(looper.java:145)                                                                       @ android.app.activitythread.main(activitythread.java:5942)                                                                       @ java.lang.reflect.method.invoke(native method)                                                                       @ java.lang.reflect.method.invoke(method.java:372)                                                                       @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1399)                                                                       @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1194)                                                                    caused by: java.lang.nullpointerexception: attempt invoke virtual method 'int android.graphics.bitmap.getwidth()' on null object reference                                                                       @ id.zelory.compressor.imageutil.getscaledbitmap(imageutil.java:62)                                                                       @ id.zelory.compressor.imageutil.compressimage(imageutil.java:161)                                                                       @ id.zelory.compressor.compressor.compresstofile(compressor.java:48)                                                                       @ com.venon.nakomangsp.signupfragment.preparepicture(signupfragment.java:870)                                                                       @ com.venon.nakomangsp.signupfragment.onactivityresult(signupfragment.java:783)                                                                       @ android.support.v4.app.fragmentactivity.onactivityresult(fragmentactivity.java:165)                                                                       @ com.venon.nakomangsp.signupactivity.onactivityresult(signupactivity.java:37)                                                                       @ android.app.activity.dispatchactivityresult(activity.java:6549)                                                                       @ android.app.activitythread.deliverresults(activitythread.java:4054)                                                                       @ android.app.activitythread.handlesendresult(activitythread.java:4101)                                                                        @ android.app.activitythread.access$1400(activitythread.java:177)                                                                        @ android.app.activitythread$h.handlemessage(activitythread.java:1497)                                                                        @ android.os.handler.dispatchmessage(handler.java:102)                                                                        @ android.os.looper.loop(looper.java:145)                                                                        @ android.app.activitythread.main(activitythread.java:5942)                                                                        @ java.lang.reflect.method.invoke(native method)                                                                        @ java.lang.reflect.method.invoke(method.java:372)                                                                        @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1399)                                                                        @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1194)  

apps not have direct file access of contents of sd card. therefore if 'path' of sort, app not have ability read location.

the way reliably access contents of these files through contentresolver.openinputstream() (or other open methods if need assetfiledescriptor, etc).

you can copy file own cache directory , whatever want image.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -