android - Get path from gallery without failing delivering Result -
my application works fine on devices. unfortunately others reported following bug failure delivering result dont know how solve
so use following code:
@override public void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == 1 && data!=null) { if (resultcode == result_ok) { uri uri = data.getdata(); string s = getpath(uri); //null string on devices if(s!=null){ file f = new file(s); } else{ //???????? } if(resultcode== result_canceled){ } } }
and 1 method called getpath:
public string getpath(uri uri) { // safety built in if( uri == null ) { return null; } // try retrieve image media store first // work images selected gallery string[] projection = { mediastore.images.media.data }; cursor cursor = managedquery(uri, projection, null, null, null); if( cursor != null ){ int column_index = cursor .getcolumnindexorthrow(mediastore.images.media.data); cursor.movetofirst(); return cursor.getstring(column_index); } return uri.getpath();
} when user click:
image = (imageview) findviewbyid(r.id.imageview7); i.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent intent1 = new intent(); intent1.settype("image/*"); intent1.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent1,"picture"), 1); } });
and lastly got logcat
java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=1, result=-1, data=intent { dat=content: flg=0x1 }} activity {****************.mainactivity}: java.lang.nullpointerexception @ android.app.activitythread.deliverresults(activitythread.java:3942) @ android.app.activitythread.handlesendresult(activitythread.java:3992) @ android.app.activitythread.access$1300(activitythread.java:156) @ android.app.activitythread$h.handlemessage(activitythread.java:1403) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:157) @ android.app.activitythread.main(activitythread.java:5872) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:858) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:674) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ java.io.file.fixslashes(file.java:185) @ java.io.file.<init>(file.java:134) @ *************.mainactivity.onactivityresult(unknown source) @ android.app.activity.dispatchactivityresult(activity.java:5535) @ android.app.activitythread.deliverresults(activitythread.java:3938) ... 11 more
what doing wrong?
try intent
intent = new intent(intent.action_pick,android.provider.mediastore.images.media.external_content_uri); startactivityforresult(i, result_load_image);
try onactivityresult:
@override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == result_load_image && resultcode == result_ok && null != data) { uri selectedimage = data.getdata(); string[] filepathcolumn = { mediastore.images.media.data }; cursor cursor = getcontentresolver().query(selectedimage,filepathcolumn, null, null, null); cursor.movetofirst(); int columnindex = cursor.getcolumnindex(filepathcolumn[0]); string picturepath = cursor.getstring(columnindex); cursor.close(); imageview imageview = (imageview) findviewbyid(r.id.imgview); imageview.setimagebitmap(bitmapfactory.decodefile(picturepath)); } }
Comments
Post a Comment