In development, invoking system libraries and cropping photos is a common requirement. Compared to the realization of this function, the direct call system has many advantages, such as not to consider screen adaptation, do not worry about performance issues, and so on. Therefore, for the general needs, it is recommended to call the function of the system directly, simple and efficient!
First up:
One, only call the system library (not cropping), return the user selected pictures. (Only support single, if you need to choose more than the implementation of their own, refer to another blog: Android: Imitation QQ photo selector (by album category display, multi-select Add))
1. Jump to the System Gallery page:
Intent i = new Intent (Intent.action_pick, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); Startactivityforresult (I, Select_image);
2. Receive the information returned by the system library in Onactivityresult (that is, the photos selected by the user).
@Override protected void onactivityresult (int requestcode, int resultcode, Intent data) { Super.onactivityresult (Requestcode, ResultCode, data); if (resultcode! = RESULT_OK | | data = NULL) { return; } Select an image if (Requestcode = = select_image) {//get image path from URI String ImagePath = Getimagepath (Data.getdata ()); return; } }
Private String Getimagepath (Uri selectedimage) { string[] Filepathcolumn = {MediaStore.Images.Media.DATA}; cursor cursor = getcontentresolver (). Query (selectedimage, filepathcolumn, NULL, NULL, NULL); Cursor.movetofirst (); int columnindex = Cursor.getcolumnindex (filepathcolumn[0]); String ImagePath = cursor.getstring (columnindex); Cursor.close (); SYSTEM.OUT.PRINTLN ("Image path:" + ImagePath); return imagePath; }
Second, jump to the system gallery, select Photos, and crop.
1. Jump to System Gallery:
Select image via System Gallery, crop and save the new image file. public void Selectimageandcrop (View View) { //after cropping, the image file would be stored here!   ; cachefile = imagecachefolder + file.separator + "cache_" + system.currenttimemillis () + ". jpg"; Intent Intent = new Intent (intent.action_get_content); Intent.settype ("image/*"); Intent.putextra (" Crop "," true "); //width:height Intent.putextra ("Aspectx", 1); Intent.putextra ("AspectY", 1); Intent.putextra ("Output", Uri.fromfile (New File (Cachefile))); Intent.putextra ("OutputFormat", "JPEG"); Startactivityforresult (Intent.createchooser (Intent, "Choose Image"), SELECT_ Image_crop); }
Data to be passed before jumping:
(1) Crop: Pass a true to tell the system to crop.
(2) Aspectx and aspecty: the width-to-height ratio of the cropping box.
(3) Output: You need to pass a URI built by the file path Cachefile, the user will automatically go to the cropping page after selecting a photo on the gallery page, and the image will be saved in the Cachefile position after cropping.
Once the cropping is complete, the Onactivityresult method is also called back (ResultCode is RESULT_OK), and the picture is saved in the Cachefile location, so you can use the file directly, for example, to set it as a ImageView resource.
@Override protected void onactivityresult (int requestcode, int resultcode, Intent data) { Super.onactivityresult (Requestcode, ResultCode, data); if (resultcode! = RESULT_OK) { return; } Select an image and crop if (Requestcode = = Select_image_crop) {//compress The original image to save M Emory. Bitmapfactory.options opt = new bitmapfactory.options (); Opt.insamplesize = 4; Imageview.setimagebitmap (Bitmapfactory.decodefile (Cachefile, opt)); } }
Android: Call system gallery/Crop Picture