java - handle the result form the gallery -
i searched many days how image gallery
admit found alot of codes no 1 worked . have code , when hit button , choose image program stop "unfortunately app has stopped " . appreciated ... code
public class mainactivity extends appcompatactivity { private static int result_load_image = 1; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button buttonloadimage = (button) findviewbyid(r.id.button); buttonloadimage.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { intent = new intent( intent.action_pick, android.provider.mediastore.images.media.external_content_uri); startactivityforresult(i, result_load_image); } }); } @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.imageview); imageview.setimagebitmap(bitmapfactory.decodefile(picturepath)); } }
}
below working solution of choosing picture library or capturing 1 camera , using in imageview.
source : so
firstly initialize imageview in oncreate
method. call selectimage
function through button's onclick
event , that's it.
//functions select image device private void selectimage() { final charsequence[] items = {"take photo","choose library", "cancel" }; alertdialog.builder builder = new alertdialog.builder(signature_new.this); builder.settitle("add photo!"); builder.setitems(items, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int item) { if (items[item].equals("take photo")) { intent intent = new intent(mediastore.action_image_capture); startactivityforresult(intent, request_camera); } else if (items[item].equals("choose library")) { intent intent = new intent( intent.action_pick, android.provider.mediastore.images.media.external_content_uri); intent.settype("image/*"); startactivityforresult( intent.createchooser(intent, "select file"), select_file); } else if (items[item].equals("cancel")) { dialog.dismiss(); } } }); builder.show(); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (resultcode == result_ok) { if (requestcode == request_camera) { bitmap thumbnail = (bitmap) data.getextras().get("data"); bytearrayoutputstream bytes = new bytearrayoutputstream(); thumbnail.compress(bitmap.compressformat.jpeg, 90, bytes); file destination = new file(environment.getexternalstoragedirectory(), system.currenttimemillis() + ".jpg"); fileoutputstream fo; try { destination.createnewfile(); fo = new fileoutputstream(destination); fo.write(bytes.tobytearray()); fo.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } imagef.setimage(imagesource.bitmap(thumbnail)); } else if (requestcode == select_file) { uri selectedimageuri = data.getdata(); try { bitmap bm=decodeuri(selectedimageuri); imageviewf.setimage(imagesource.bitmap(bm)); //uploadbm=bm; //dialog_dimension(); } catch(filenotfoundexception e) { e.printstacktrace(); } } } }
Comments
Post a Comment