Picking image from gallery on Nexus 5 (Android 5.1.1) returning null, working on other devices -
i have app has function allows user pick image gallery. here code doing that:
@onclick(r.id.imageview_fragmentuploadlayout_uploadedart) public void setuploadedartimageview () { //startactivityforresult(new intent(intent.action_pick, mediastore.images.media.internal_content_uri), get_from_gallery); intent intent = new intent(); // show images, no videos or else intent.settype("image/*"); intent.setaction(intent.action_get_content); // show chooser (if there multiple options available) startactivityforresult(intent.createchooser(intent, "select picture"), get_from_gallery); }
and onactivityresult method:
public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); //detects request codes if(requestcode == get_from_gallery && resultcode == activity.result_ok && data != null) { selectedimageuri = data.getdata(); selectedimagepath = getpath(selectedimageuri); file = new file(selectedimagepath); //parsefile parseimage = new parsefile(); picasso.with(getactivity()) .load(selectedimageuri.tostring()) .resize(500, 500) .centercrop() .noplaceholder() .into(uploadedartimageview); uploadedartimageviewplaceholder.setvisibility(view.gone); imageresizer.resize(file, 640, 480, resizemode.fit_exact); } }
getpath method:
public string getpath(uri uri) { // safety built in if( uri == null ) { // todo perform logging or show user feedback return null; } // try retrieve image media store first // work images selected gallery string[] projection = { mediastore.images.media.data }; cursor cursor = getactivity().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); } // our fallback here return uri.getpath(); }
if test on samsung s2 works, (currently on cyanogen mod, android 4.0.4), if test on nexus 5 (android 5.1.1) return null pointer exception.
note
i have tried different apps: thought getting picture gallery google photos might have cause problem, installed on s2 , works.
here stacktrace:
java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=131075, result=-1, data=intent { dat=content://com.android.providers.media.documents/document/image:25147 flg=0x1 }} activity {com.entu.artapp/com.entu.artapp.activity.fragmentmanager}: java.lang.nullpointerexception: attempt invoke virtual method 'char[] java.lang.string.tochararray()' on null object reference @ android.app.activitythread.deliverresults(activitythread.java:3574) @ android.app.activitythread.handlesendresult(activitythread.java:3617) @ android.app.activitythread.access$1300(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1352) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(method.java) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'char[] java.lang.string.tochararray()' on null object reference @ java.io.file.fixslashes(file.java:185) @ java.io.file.<init>(file.java:134) @ com.entu.artapp.fragments.uploadpicturefragment.onactivityresult(uploadpicturefragment.java:363) @ android.support.v4.app.fragmentactivity.onactivityresult(fragmentactivity.java:163) @ android.app.activity.dispatchactivityresult(activity.java:6192) @ android.app.activitythread.deliverresults(activitythread.java:3570) @ android.app.activitythread.handlesendresult(activitythread.java:3617) @ android.app.activitythread.access$1300(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1352) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(method.java) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
can me problem?
thanks in advance, cheers!
a uri
not file. cannot reliably local path user picks via action_get_content
. need treat uri
url web server, open streams on uri
, whether directly (getcontentresolver().openinputstream()
) or indirectly (via picasso).
i not know imageresizer
is, if works files, stop using it. delete getpath()
method , work solely uri
.
Comments
Post a Comment