i new android. want set image on imageview gallery , captured image camera.when click on change photo button there dialog box , gallery or camera open image selected not set on imageview.please help.
public class profile extends appcompatactivity implements view.onclicklistener{ private static final int select_file = 1; button logout; button gohome; button changepic; imageview ivprofile; private session session; string userchoosentask; private static final int request_camera = 1888; void initviews(){ logout = (button)findviewbyid(r.id.buttonlogout); gohome = (button)findviewbyid(r.id.buttongohome); changepic = (button)findviewbyid(r.id.buttonchangepic); session = new session(this); logout.setonclicklistener(this); gohome.setonclicklistener(this); changepic.setonclicklistener(this); if (!session.loggedin()){ logout(); } } private void logout() { session.setloggedin(false); finish(); intent = new intent(profile.this, login.class); startactivity(i); } private void selectimage() { final charsequence[] items = { "take photo", "choose library", "cancel" }; alertdialog.builder builder = new alertdialog.builder(profile.this); builder.settitle("add photo!"); builder.setitems(items, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int item) { boolean result=utility.checkpermission(profile.this); if (items[item].equals("take photo")) { userchoosentask="take photo"; if(result) cameraintent(); } else if (items[item].equals("choose library")) { userchoosentask="choose library"; if(result) galleryintent(); } else if (items[item].equals("cancel")) { dialog.dismiss(); } } }); builder.show(); } private void cameraintent() { intent intent = new intent(mediastore.action_image_capture); startactivityforresult(intent,request_camera); } private void galleryintent() { intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, "select file"),select_file); } @override public void onrequestpermissionsresult(int requestcode, string[] permissions, int[] grantresults) { switch (requestcode) { case utility.my_permissions_request_read_external_storage: if (grantresults.length > 0 && grantresults[0] == packagemanager.permission_granted) { if(userchoosentask.equals("take photo")) cameraintent(); else if(userchoosentask.equals("choose library")) galleryintent(); } else { } break; } } @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (resultcode == activity.result_ok) { if (requestcode == select_file) onselectfromgalleryresult(data); if (requestcode == request_camera) { oncaptureimageresult(data); } } } private void onselectfromgalleryresult(intent data) { bitmap bm=null; if (data != null) { try { bm = mediastore.images.media.getbitmap(getapplicationcontext() .getcontentresolver(), data.getdata()); } catch (ioexception e) { e.printstacktrace(); } } ivprofile.setimagebitmap(bm); } private void oncaptureimageresult(intent data) { 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(); } ivprofile.setimagebitmap(thumbnail); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_profile); string username = getintent().getstringextra("username"); textview tvusername = (textview)findviewbyid(r.id.textviewusername); tvusername.settext(username); initviews(); } @targetapi(build.version_codes.lollipop) @override public void onclick(view v) { if( v.getid() == r.id.buttonlogout){ logout(); } else if (v.getid() == r.id.buttongohome){ intent = new intent(profile.this,home.class); startactivity(i); } else if (v.getid() == r.id.buttonchangepic){ selectimage(); } } }
below code can used taking , picking photo , selection of either pick photo or take photo. show dialog 2 option , upon selection use appropriate code.
to take picture camera:
intent takepicture = new intent(mediastore.action_image_capture); startactivityforresult(takepicture, 0);//zero can replaced action code
to pick photo gallery:
intent pickphoto = new intent(intent.action_pick, android.provider.mediastore.images.media.external_content_uri); startactivityforresult(pickphoto , 1);//one can replaced action code
onactivityresult code:
protected void onactivityresult(int requestcode, int resultcode, intent imagereturnedintent) { super.onactivityresult(requestcode, resultcode, imagereturnedintent); switch(requestcode) { case 0: if(resultcode == result_ok){ bundle extras = imagereturnedintent.getextras(); bitmap imagebitmap = (bitmap) extras.get("data"); imageview.setimagebitmap(imagebitmap); } break; case 1: if(resultcode == result_ok){ uri selectedimage = imagereturnedintent.getdata(); imageview.setimageuri(selectedimage); } break; } }
finally add permission in manifest file:
<uses-permission android:name="android.permission.write_external_storage" />
Comments
Post a Comment