android croped image is too small -
i using image cropper app in android devices crop image camera or gallery
images convert string , send server on server, image size 160x160, device samsung galaxy tab2 10.1
whats wrong on used code?
resize code show thumb return 120x120 image , not send thumb server
private string setbitmaptostring(bitmap bmp) { string base64string = null; if (bmp == null) { bmp = bitmapfactory.decoderesource(getapplicationcontext().getresources(), r.drawable.app_icon); } bytearrayoutputstream baos = new bytearrayoutputstream(); bmp.compress(bitmap.compressformat.jpeg, 100, baos); byte[] imgbytes = baos.tobytearray(); base64string = base64.encodetostring(imgbytes, base64.default); contentvalues initialvalues = new contentvalues(); initialvalues.put("picture", base64string); // save base64string db return base64string; }
i using following method take picture or pick device , crop:
//==================== take picture arraylist<string> images = new arraylist<string>(); private bitmap photo; private uri mimagecaptureuri; private static final int pick_from_camera = 1; private static final int crop_from_camera = 2; private static final int pick_from_file = 3; private void takepicture(){ final string[] items = new string[]{app.context.getstring(r.string.select_from_camera), app.context.getstring(r.string.select_from_gallery)}; arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.select_dialog_item, items); alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("انتخاب تصویر"); builder.setadapter(adapter, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int item) { //pick camera if (item == 0) { intent intent = new intent(mediastore.action_image_capture); mimagecaptureuri = uri.fromfile(new file(environment.getexternalstoragedirectory(), "avatar_" + string.valueof(system.currenttimemillis()) + ".jpg")); intent.putextra(android.provider.mediastore.extra_output, mimagecaptureuri); try { intent.putextra("return-data", true); startactivityforresult(intent, pick_from_camera); } catch (activitynotfoundexception e) { e.printstacktrace(); } } else { //pick file intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, app.context.getstring(r.string.complete_action_using)), pick_from_file); } } }); final alertdialog dialog = builder.create(); rlimage1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { lastselect = 1; dialog.show(); } }); rlimage2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { lastselect = 2; dialog.show(); } }); rlimage3.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { lastselect = 3; dialog.show(); } }); rlimage4.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { lastselect = 4; dialog.show(); } }); txtselectlogonj.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { lastselect = 5; dialog.show(); } }); } private int lastselect = 4; string image1 = "", image2 = "", image3 = "", image4 = "", image5 = ""; @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (resultcode != result_ok) return; switch (requestcode) { case pick_from_camera: docrop(); break; case pick_from_file: mimagecaptureuri = data.getdata(); docrop(); break; case crop_from_camera: bundle extras = data.getextras(); if (extras != null) { photo = extras.getparcelable("data"); int w = (int) (uihelpers.width * 0.15); file f = new file(mimagecaptureuri.getpath()); if (f.exists()) { //f.delete(); } log.d("takepicture", "file path : " + f.getabsolutepath() + " , size : " + w); switch (lastselect){ case 5: txtselectlogonj.setvisibility(view.gone); rllogonj.setvisibility(view.visible); image5 = ""+setbitmaptostring(photo); app.sharedpreferences = getsharedpreferences(app.mypreferences, context.mode_private); app.editor = app.sharedpreferences.edit(); app.editor.putstring("image5", image5); app.editor.commit(); imglogonj.setimagebitmap(bitmap.createscaledbitmap(photo, w, w, false)); break; case 4: image4 = ""+setbitmaptostring(photo); app.sharedpreferences = getsharedpreferences(app.mypreferences, context.mode_private); app.editor = app.sharedpreferences.edit(); app.editor.putstring("image4", image4); app.editor.commit(); imgimage4.setimagebitmap(bitmap.createscaledbitmap(photo, w, w, false)); txtimage4.setvisibility(view.visible); break; case 3: image3 = ""+setbitmaptostring(photo); app.sharedpreferences = getsharedpreferences(app.mypreferences, context.mode_private); app.editor = app.sharedpreferences.edit(); app.editor.putstring("image3", image3); app.editor.commit(); imgimage3.setimagebitmap(bitmap.createscaledbitmap(photo, w, w, false)); txtimage3.setvisibility(view.visible); break; case 2: image2 = ""+setbitmaptostring(photo); app.sharedpreferences = getsharedpreferences(app.mypreferences, context.mode_private); app.editor = app.sharedpreferences.edit(); app.editor.putstring("image2", image2); app.editor.commit(); imgimage2.setimagebitmap(bitmap.createscaledbitmap(photo, w, w, false)); txtimage2.setvisibility(view.visible); break; case 1: image1 = ""+setbitmaptostring(photo); app.sharedpreferences = getsharedpreferences(app.mypreferences, context.mode_private); app.editor = app.sharedpreferences.edit(); app.editor.putstring("image1", image1); app.editor.commit(); imgimage1.setimagebitmap(bitmap.createscaledbitmap(photo, w, w, false)); txtimage1.setvisibility(view.visible); break; } } break; } } private void docrop() { final arraylist<cropoption> cropoptions = new arraylist<cropoption>(); intent intent = new intent("com.android.camera.action.crop"); intent.settype("image/*"); list<resolveinfo> list = getpackagemanager().queryintentactivities(intent, 0); int size = list.size(); if (size == 0) { toast.maketext(this, getapplicationcontext().getstring(r.string.crop_unavailable), toast.length_short).show(); // return } else { intent.setdata(mimagecaptureuri); intent.putextra("outputx", uihelpers.width); intent.putextra("outputy", uihelpers.width); intent.putextra("aspectx", 1); intent.putextra("aspecty", 1); intent.putextra("scale", true); intent.putextra("return-data", true); if (size == 1) { intent = new intent(intent); resolveinfo res = list.get(0); i.setcomponent(new componentname(res.activityinfo.packagename, res.activityinfo.name)); startactivityforresult(i, crop_from_camera); } else { (resolveinfo res: list) { final cropoption co = new cropoption(); co.title = getpackagemanager().getapplicationlabel(res.activityinfo.applicationinfo); co.icon = getpackagemanager().getapplicationicon(res.activityinfo.applicationinfo); co.appintent = new intent(intent); co.appintent.setcomponent(new componentname(res.activityinfo.packagename, res.activityinfo.name)); cropoptions.add(co); } cropoptionadapter adapter = new cropoptionadapter(getapplicationcontext(), cropoptions); alertdialog.builder builder = new alertdialog.builder(this); builder.settitle(getapplicationcontext().getstring(r.string.select_crop_app)); builder.setadapter(adapter, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int item) { startactivityforresult(cropoptions.get(item).appintent, crop_from_camera); } }); builder.setoncancellistener(new dialoginterface.oncancellistener() { @override public void oncancel(dialoginterface dialog) { if (mimagecaptureuri != null) { getcontentresolver().delete(mimagecaptureuri, null, null); mimagecaptureuri = null; } } }); alertdialog alert = builder.create(); alert.show(); } } } //==================== take picture
and cropoption , cropoptionadapter :
cropoptionadapter:
public class cropoptionadapter extends arrayadapter<cropoption> { private arraylist<cropoption> moptions; private layoutinflater minflater; public cropoptionadapter(context context, arraylist<cropoption> options) { super(context, r.layout.crop_selector, options); moptions = options; minflater = layoutinflater.from(context); } @override public view getview(int position, view convertview, viewgroup group) { if (convertview == null) convertview = minflater.inflate(r.layout.crop_selector, null); cropoption item = moptions.get(position); if (item != null) { ((imageview) convertview.findviewbyid(r.id.iv_icon)).setimagedrawable(item.icon); ((textview) convertview.findviewbyid(r.id.tv_name)).settext(item.title); return convertview; } return null; } }
cropoption:
public class cropoption { public charsequence title; public drawable icon; public intent appintent; }
edited using gallery crop, using "bitmap photo" return small image, using
file f = new file(mimagecaptureuri.getpath());
f.getabsolutepath()
but until sending image server, show outofmemory error
that's new problem
out of memory issue can solved fixing cropped/image gallery image fixed size. in onactivityresult include
//temp file save cropped image private string mimagepath; private uri msaveuri = null; private uri mimageuri = null; //file capturing camera images private file mfiletemp; if (resultcode == result_ok) { try { inputstream inputstream = getcontentresolver().openinputstream(result.getdata()); // got bitmap .. copy temp file cropping fileoutputstream fileoutputstream = new fileoutputstream(mfiletemp); copystream(inputstream, fileoutputstream); fileoutputstream.close(); inputstream.close(); mimagepath = mfiletemp.getpath(); msaveuri = utils.getimageuri(mimagepath); mimageuri = utils.getimageuri(mimagepath); init(); } catch (exception e) { errored(); } private static void copystream(inputstream input, outputstream output) throws ioexception { byte[] buffer = new byte[1024]; int bytesread; while ((bytesread = input.read(buffer)) != -1) { output.write(buffer, 0, bytesread); } } public class utils { public static uri getimageuri(string path) { return uri.fromfile(new file(path)); }
}
if use custom libraries, have created 1 , shared in github
Comments
Post a Comment