Android Select Picture trim take photo compatible with all versions of code

Source: Internet
Author: User

In the project, always avoid the portrait of the cut and selection, however, after 4.4 can be used before the code, also can not be used. Make a note of it. Because I used the code a few times.

Copy and paste directly later.


1. Take photos and select pictures to select pictures
<span style= "White-space:pre" ></span>intent = new Intent (intent.action_get_content); Intent.setType (" image/* "); Startactivityforresult (Intent, Gallery_request_code);



Photo
<span style= "White-space:pre" ></span>intent = new Intent (mediastore.action_image_capture); Startactivityforresult (Intent, Camera_request_code);

2. Get the value tag that came with the system
private static int camera_request_code = 1;private static int gallery_request_code = 2;private static int crop_request_cod E = 3;



@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (Requestcode = = Camera_request _code) {if (data = = null) {return;} else {//photo bundle Extras = Data.getextras (); if (extras! = null) {Bitmap BM = extras.ge Tparcelable ("Data"); Uri uri = Savebitmap (BM); Startimagezoom (URI);}}} else if (Requestcode = = Gallery_request_code) {if (data = = NULL) {//album return;} Uri Uri;uri = Data.getdata (); Uri Fileuri = Converturi (URI); Startimagezoom (Fileuri);} else if (Requestcode = = Crop_request_code) {if (data = = null) {return;} Clipped picture Bundle extras = Data.getextras (); if (extras = = null) {return;} Bitmap BM = extras.getparcelable ("Data"); Showimageview (BM);}}

The picture is selected and then converted to stream and saved according to the URL
Private URI Converturi (Uri uri) {InputStream = null;try {is = Getcontentresolver (). Openinputstream (URI); Bitmap Bitmap = Bitmapfactory.decodestream (is); Is.close (); return Savebitmap (Bitmap);} catch (FileNotFoundException e) {e.printstacktrace (); return null;} catch (IOException e) {e.printstacktrace (); return null;}}

Save Picture Remember add permission
Private Uri Savebitmap (Bitmap BM) {File TmpDir = new File (environment.getexternalstoragedirectory () + "/xiaoxin"); Tmpdir.exists ()) {Tmpdir.mkdir ();} File img = new file (Tmpdir.getabsolutepath () + "love.png"), try {fileoutputstream fos = new FileOutputStream (IMG); bm.compr ESS (Bitmap.CompressFormat.PNG, Fos.flush, FOS); Fos.close (); Toast.maketext (Mainactivity.this, "succeeded", Toast.length_short). Show (); return Uri.fromfile (IMG); catch (FileNotFoundException e) {toast.maketext (Mainactivity.this, "failed", Toast.length_short). Show (); E.printstacktrace (); return null;} catch (IOException e) {e.printstacktrace (); Toast.maketext (Mainactivity.this, "failed", Toast.length_short). Show (); return null;}}


3. Crop the picture
/** * Clip Picture *  * @param uri */private void Startimagezoom (Uri uri) {Intent Intent = new Intent ("Com.android.camera.actio N.crop "); Intent.setdataandtype (URI," image/* "); Intent.putextra (" CROP "," true "); Intent.putextra (" Aspectx ", 1); Intent.putextra ("Aspecty", 1), Intent.putextra ("Outputx", "Intent.putextra"; Return-data ", true); Startactivityforresult (Intent, crop_request_code);}

4. Conclusion: Remember to add permission Oh, after the picture is trimmed, there will be a Bitmap object. You can use it to set it up to ImageView. can also be transferred directly to the server. If you have any questions, please leave a message

Android Select Picture trim take photo compatible with all versions of code

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.