Sunday 15 May 2011

image - Android camera.action.CROP doesn't give consistent results -



image - Android camera.action.CROP doesn't give consistent results -

i trying pick , crop image straight gallery, reason crop intent doesn't work, on devices working stock gallery, , on devices working unofficial gallery apps (like g2).

i know should work on device because have other apps utilize same gallery crop function , works.

every time seek open intent stock gallery next message: couldn't find item (i want user pick image, not pass specific image location)

// start intent event image user private void getcroppedimage(){ // create output directory image string timestamp = new simpledateformat("ddmmmyy_hhmmsss").format(new date()); file dir = new file(environment.getexternalstoragedirectory () + file.separator + "myapp/images"); file picpath = environment.getexternalstoragepublicdirectory( environment.directory_pictures); file outputpath = new file(dir,"img_"+ timestamp + ".jpg"); // create sure output dir exists if (!dir.exists()){ dir.mkdirs(); } // start intent, if available intent cropintent = new intent("com.android.camera.action.crop"); //cropintent.setdata(uri.fromfile(picpath)); cropintent.settype("image/*"); cropintent.putextra("crop", "true"); cropintent.putextra("outputx", 512); cropintent.putextra("outputy", 512); cropintent.putextra("aspectx", 1); cropintent.putextra("aspecty", 1); cropintent.putextra("output", uri.fromfile(outputpath)); // verify intent resolve activity if (cropintent.resolveactivity(getpackagemanager()) != null) { startactivityforresult(cropintent, request_code_crop_image); }else{ cropintent.setaction(intent.action_get_content); // verify intent resolve activity if (cropintent.resolveactivity(getpackagemanager()) != null) { startactivityforresult(cropintent, request_code_get_picture); }else{ toast.maketext(this,"gallery not detected",toast.length_long).show(); } } }

any thoughts?

try this:

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); homecoming convertview; } homecoming null; } } public class cropoption { public charsequence title; public drawable icon; public intent appintent; } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (resultcode != result_ok) return; switch (requestcode) { case pick_from_camera: /** * after taking picture, crop */ docrop(); break; case pick_from_file: /** * after selecting image files, save selected path */ mimagecaptureuri = data.getdata(); infolog("pick file"); docrop(); break; case crop_from_camera: bundle extras = data.getextras(); /** * after cropping image, bitmap of cropped image , * display on imageview. */ if (extras != null) { photo = extras.getparcelable("data"); bitmap circlebitmap = bitmap.createbitmap(photo.getwidth(), photo.getheight(), bitmap.config.argb_8888); bitmapshader shader = new bitmapshader(photo, tilemode.clamp, tilemode.clamp); paint paint = new paint(); paint.setshader(shader); canvas c = new canvas(circlebitmap); c.drawcircle(photo.getwidth() / 2, photo.getheight() / 2, photo.getwidth() / 2, paint); img.setimagebitmap(photo); } file f = new file(mimagecaptureuri.getpath()); /** * delete temporary image */ if (f.exists()) f.delete(); break; } } private void docrop() { final arraylist<cropoption> cropoptions = new arraylist<cropoption>(); /** * open image crop app starting intent * ‘com.android.camera.action.crop‘. */ intent intent = new intent("com.android.camera.action.crop"); intent.settype("image/*"); /** * check if there image cropper app installed. */ list<resolveinfo> list = getpackagemanager().queryintentactivities( intent, 0); int size = list.size(); /** * if there no image cropper app, display warning message */ if (size == 0) { toast.maketext(this, "can not find image crop app", toast.length_short).show(); return; } else { /** * specify image path, crop dimension , scale */ intent.setdata(mimagecaptureuri); intent.putextra("outputx", 200); intent.putextra("outputy", 200); intent.putextra("aspectx", 1); intent.putextra("aspecty", 1); intent.putextra("scale", true); intent.putextra("return-data", true); /** * there possibility when more 1 image cropper app exist, * have check first. if there 1 app, open * app. */ 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 { /** * if there several app exist, create custom chooser * allow user selects app. */ (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("choose crop app"); builder.setadapter(adapter, new dialoginterface.onclicklistener() { 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(); } } }

android image android-intent gallery crop

No comments:

Post a Comment