Monday, 15 April 2013

How to uplaod high quality image to imageview in android? -



How to uplaod high quality image to imageview in android? -

i want upload high quality image server , in imageview in app. i'm able so. problem quality of image low. how can upload want resolution of image server , in imageview. help me.

here code:

public class changeprofileactivity extends activity { imageview updatebt, iv; private sharedpreferences saveimagepref; private sharedpreferences.editor saveimageprefeditor; protected static final int camera_request = 0; protected static final int gallery_picture = 1; private intent pictureactionintent = null; bitmap bitmap; private touchimageview touch; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.requestwindowfeature(window.feature_no_title); // setrequestedorientation(activityinfo.screen_orientation_portrait); setcontentview(r.layout.activity_change_profile); updatebt = (imageview) findviewbyid(r.id.buttonupdate); dialog = new alertdialogcheck(changeprofileactivity.this); iv = (imageview) findviewbyid(r.id.personalpictureiv); saveimagepref = getsharedpreferences("saveimage", mode_private); saveimageprefeditor = saveimagepref.edit(); savedobpref = getsharedpreferences("saveimage", mode_private); savedobprefeditor = saveimagepref.edit(); registerforcontextmenu(iv); = saveimagepref.getstring("save", "0"); bitmap = stringtobitmap(a); if (bitmap != null) { bitmap = bitmap.createscaledbitmap(bitmap, 250, 250, false); iv.setimagebitmap(bitmap); } if (bitmap == null) { bitmap = bitmapfactory.decoderesource(this.getresources(), r.drawable.user_profile_image); iv.setimagebitmap(bitmap); iv.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { startdialog(); uploadimage(); } }); } iv.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub startdialog(); } }); public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.main, menu); } private void startdialog() { alertdialog.builder myalertdialog = new alertdialog.builder(this); myalertdialog.settitle("upload pictures option"); myalertdialog.setmessage("how want set picture?"); myalertdialog.setpositivebutton("gallery", new dialoginterface.onclicklistener() { public void onclick(dialoginterface arg0, int arg1) { pictureactionintent = new intent( intent.action_get_content, null); pictureactionintent.settype("image/*"); pictureactionintent.putextra("return-data", true); startactivityforresult(pictureactionintent, gallery_picture); } }); myalertdialog.setnegativebutton("camera", new dialoginterface.onclicklistener() { public void onclick(dialoginterface arg0, int arg1) { pictureactionintent = new intent( android.provider.mediastore.action_image_capture); startactivityforresult(pictureactionintent, camera_request); } }); myalertdialog.show(); } protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == gallery_picture) { if (resultcode == result_ok) { if (data != null) { // our bitmapdrawable thumbnail bitmapdrawable bmpdrawable = null; // seek retrieve image using info intent cursor cursor = getcontentresolver().query(data.getdata(), null, null, null, null); if (cursor != null) { cursor.movetofirst(); int idx = cursor.getcolumnindex(imagecolumns.data); string filesrc = cursor.getstring(idx); bitmapfactory.options options=new bitmapfactory.options(); options.insamplesize=1; bitmap = bitmapfactory.decodefile(filesrc); bitmap = bitmap.createscaledbitmap(bitmap, 250, 250, true); saveimageprefeditor.clear(); saveimageprefeditor.commit(); saveimageprefeditor.putstring("save", bitmaptostring(bitmap)); saveimageprefeditor.commit(); iv.setimagebitmap(bitmap); // touch.setimagebitmap(bitmap);+ = saveimagepref.getstring("save", "0"); uploadimage(); } else { bmpdrawable = new bitmapdrawable(getresources(), info .getdata().getpath()); iv.setimagedrawable(bmpdrawable); // touch.setimagebitmap(bitmap); = saveimagepref.getstring("save", "0"); uploadimage(); } } else { toast.maketext(getapplicationcontext(), "cancelled", toast.length_short).show(); } } else if (resultcode == result_canceled) { toast.maketext(getapplicationcontext(), "cancelled", toast.length_short).show(); } } else if (requestcode == camera_request) { if (resultcode == result_ok) { if (data.hasextra("data")) { bitmap = (bitmap) data.getextras().get("data"); bitmap = bitmap.createscaledbitmap(bitmap, 250, 250, false); saveimageprefeditor.clear(); saveimageprefeditor.commit(); saveimageprefeditor.putstring("save", bitmaptostring(bitmap)); saveimageprefeditor.commit(); // update image view bitmap iv.setimagebitmap(bitmap); // touch.setimagebitmap(bitmap); = saveimagepref.getstring("save", "0"); uploadimage(); } else if (data.getextras() == null) { toast.maketext(getapplicationcontext(), "no extras retrieve!", toast.length_short) .show(); bitmapdrawable thumbnail = new bitmapdrawable( getresources(), data.getdata().getpath()); // update image view newly created drawable iv.setimagedrawable(thumbnail); // touch.setimagebitmap(bitmap); = saveimagepref.getstring("save", "0"); uploadimage(); } } else if (resultcode == result_canceled) { toast.maketext(getapplicationcontext(), "cancelled", toast.length_short).show(); } } } public string bitmaptostring(bitmap bitmap) { bytearrayoutputstream baos = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.jpeg, 100, baos); byte[] b = baos.tobytearray(); string temp = base64.encodetostring(b, base64.default); homecoming temp; } public bitmap stringtobitmap(string encodedstring) { seek { byte[] encodebyte = base64.decode(encodedstring, base64.default); bitmap bitmap = bitmapfactory.decodebytearray(encodebyte, 0, encodebyte.length); homecoming bitmap; } grab (exception e) { e.getmessage(); homecoming null; }} }

you can utilize android best practice.

public static int calculateinsamplesize( bitmapfactory.options options, int reqwidth, int reqheight) { // raw height , width of image final int height = options.outheight; final int width = options.outwidth; int insamplesize = 1; if (height > reqheight || width > reqwidth) { final int halfheight = height / 2; final int halfwidth = width / 2; // calculate largest insamplesize value powerfulness of 2 , keeps both // height , width larger requested height , width. while ((halfheight / insamplesize) > reqheight && (halfwidth / insamplesize) > reqwidth) { insamplesize *= 2; } } homecoming insamplesize; } public static bitmap decodesampledbitmapfromresource(resources res, int resid, int reqwidth, int reqheight) { // first decode injustdecodebounds=true check dimensions final bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decoderesource(res, resid, options); // calculate insamplesize options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight); // decode bitmap insamplesize set options.injustdecodebounds = false; homecoming bitmapfactory.decoderesource(res, resid, options); } public static bitmap decodefile(int width , int height ,file f) { seek { // decode image size bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; bitmapfactory.decodestream(new fileinputstream(f), null, o); // decode insamplesize bitmapfactory.options o2 = new bitmapfactory.options(); o2.insamplesize = calculateinsamplesize(o, width, height); //system.out.println("scale: " + scale); homecoming bitmapfactory.decodestream(new fileinputstream(f), null, o2); } grab (filenotfoundexception e) { //system.out.println("--- exception "+e.tostring()); e.printstacktrace(); } homecoming null; }

android image bitmap image-uploading

No comments:

Post a Comment