Android Picture Clipping solution--from the album screenshot

Source: Internet
Author: User

In Android development, it is easy to call a intent to complete the work from the album:

New NULL ); Intent.settype ("image/*"); Intent.putextra ("crop", "true");

The additional options are as follows:

tr>
options data type description
crop String send clipping signal
aspectx int x-directional scale
aspecty int y-direction The proportions of
outputx int clipping area
outputy in T height of crop area
scale boolean whether to maintain proportions
ret Urn-data boolean to leave data in bitmap
data parcelable corresponding bitmap data
circlecrop String circle cropped area?
mediastore.extra_output ("OUTPUT") URI points the URI to the corresponding file:///...

This return-data is more puzzling.

There are two methods of receiving data:

method One: set Return-data to True so that the data is returned directly to Onactivityresult via intent, as follows:

 Public voidselectsmallimg (view view) {Intent Intent=NewIntent (Intent.action_get_content,NULL); Intent.settype ("Image/*"); Intent.putextra ("Crop", "true"); Intent.putextra ("Aspectx", 1); Intent.putextra ("Aspecty", 1); Intent.putextra ("Outputx", 800); Intent.putextra ("Outputy", 800); Intent.putextra ("Scale",true); Intent.putextra ("Return-data",true); Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection",true);//No Face DetectionStartactivityforresult (Intent, choose_small_picture); }

Method Two: Return-data is set to False, the mediastore.extra_output tag is used to store the data in an external temporary URI, which is then read by Onactivityresult. Of course, in advance to prepare a point to the file Uri, if there is sdcard is not a problem, what will be????

 Public voidselectlargeimg (view view) {//This was for Android 4.4?? //Intent Intent = new Intent (intent.action_open_document);Intent Intent =NewIntent (Intent.action_get_content,NULL); Intent.settype ("Image/*"); Intent.putextra ("Crop",true); Intent.putextra ("Aspectx", 1); Intent.putextra ("Aspecty", 1); //Intent.putextra ("Outputx", 400); //the X-pixel size when the data is returned. //Intent.putextra ("Outputy", 400); //returns the pixel size of Y. Intent.putextra ("scale",true);Intent.putextra ("Return-data",false);        Intent.putextra (Mediastore.extra_output, Imageuri); Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection",true);    Startactivityforresult (intent,choose_big_picture); }

This can be done in Onactivityresult:

@Override Public voidOnactivityresult (intRequestcode,intResultCode, Intent resultdata) {        Switch(requestcode) { Casechoose_big_picture:if(ResultCode = = Activity.result_ok && Resultdata! =NULL) {log.i (debug_flag,imageuri.tostring ()); Bitmap Bitmap= Decodeuriasbitmap (Imageuri);//decode Bitmap (write a function to convert the URI to bitmap)                    if(Bitmap = =NULL) {log.i (Debug_flag,"Big_picture NULL"); }Else{imageview.setimagebitmap (bitmap); LOG.I (Debug_flag,"Size:" +bitmap.getwidth () + "X" +bitmap.getheight ()); }                                    }                 Break;  Casechoose_small_picture:if(ResultCode = = Activity.result_ok && Resultdata! =NULL) {Bitmap Bitmap= Resultdata.getparcelableextra ("Data"); if(Bitmap = =NULL) {log.i (Debug_flag,"Small_picture NULL"); }Else{imageview.setimagebitmap (bitmap); LOG.I (Debug_flag,"Size:" +bitmap.getwidth () + "X" +bitmap.getheight ()); }                }                 Break; default :                 Break; }    }

Write yourself a function to convert the URI to bitmap:

Bitmap decodeuriasbitmap (Uri uri) {            null;             Try {                = Bitmapfactory.decodestream (Getcontentresolver (). Openinputstream (URI));             Catch (FileNotFoundException e) {                e.printstacktrace ();                 return NULL ;            }             return bitmap;    }

============================

To summarize: memory-based considerations, when the image is large, the Android function will return a 160*160 thumbnail, so that only through the external URI to receive data, if it is a small graph, the direct use of intent to pass data is no problem.

Android Picture clipping solution-from albums

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.