java - Choose Image in Gallery -
i have code, when click @ button gallery open , when choose image gallery close again. , nothing happen.
public class mainactivity extends appcompatactivity {
private button loadimagebutton; private imageview imageview; private static int result_load_image = 1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); loadimagebutton = (button) findviewbyid(r.id.button); loadimagebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent = new intent(intent.action_pick, android.provider.mediastore.images.media.external_content_uri); startactivityforresult(i, result_load_image); } }); } 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)findviewbyid(r.id.imageview); imageview.setimagebitmap(bitmapfactory.decodefile(picturepath)); } else{ toast.maketext(getapplicationcontext(), "error", toast.length_long).show(); } } what wrong?
i hope :
private string getrealpathfromuri(uri contenturi) { string result = ""; cursor cursor = getcontentresolver().query(contenturi, null, null, null, null); if (cursor == null) { result = contenturi.getpath(); } else { cursor.movetofirst(); int idx = cursor.getcolumnindex(mediastore.images.imagecolumns.data); result = cursor.getstring(idx); cursor.close(); } return result; } and in onactivityresult();
imageview = (imageview)findviewbyid(r.id.imageview); imageview.setimagebitmap(bitmapfactory.decodefile(getrealpathfromuri(uri)));
Comments
Post a Comment