android - onActivityResult data always returns null inside fragment -
gallery.class
import android.content.intent; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.relativelayout; public class gallery extends fragment { private static final int pick_from_gallery = 1; relativelayout gallerylayout; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.mainfragment, container, false); gallerylayout = (relativelayout) v.findviewbyid(r.id.gallery_layout); gallerylayout.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { firegallery(); } }); return v; } private void firegallery() { intent intent = new intent(); intent.settype("image/*"); intent.putextra(intent.extra_allow_multiple, true); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, "select picture"), pick_from_gallery); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { switch (requestcode) { case pick_from_gallery: string[] all_path = data.getstringarrayextra("all_path"); system.out.println("all_path " + all_path); //returns null system.out.println("data " + data.getextras()); //returns null break; } } }
the data in onactivityresult
null, please correct me if wrong code. mentioned both logs inside onactivityresult
returns null. note extending fragment not activity.
try this, may data it:
@override protected void onactivityresult (int requestcode,int resultcode,intent data){ super.onactivityresult (requestcode,resultcode,data); try{ // when image picked if (requestcode == pick_from_gallery && resultcode == result_ok && null != data){ // image data uri selectedimage = data.getdata (); string[] filepathcolumn = {mediastore.images.media.data}; // cursor cursor cursor = getcontentresolver ().query (selectedimage, filepathcolumn,null,null,null); // move first row cursor.movetofirst (); int columnindex = cursor.getcolumnindex (filepathcolumn[0]); string imgdecodablestring = cursor.getstring (columnindex); cursor.close (); log.e ("image path",imgdecodablestring); toast.maketext (this,"you have picked image" , toast.length_long).show (); } else{ toast.maketext (this,"you haven't picked image", toast.length_long).show (); } } catch (exception e){ toast.maketext (this,"something went wrong",toast.length_long) .show (); log.e ("exception",e.tostring ()); } }
Comments
Post a Comment