Android calls photo album to achieve system control scaling and image Cutting

Source: Internet
Author: User

In android, if you want to process images, you can call the system controls to achieve very good effects of scaling and cutting images. Today, I wrote a demo to share with you.

Package cn. m15.test; import java. io. byteArrayOutputStream; import java. io. file; import android. app. activity; import android. content. intent; import android. graphics. bitmap; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediaStore; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. Widget. imageView; public class testActivity extends Activity {public static final int NONE = 0; public static final int PHOTOHRAPH = 1; // photograph public static final int PHOTOZOOM = 2; // scale public static final int PHOTORESOULT = 3; // result public static final String IMAGE_UNSPECIFIED = "image/*"; ImageView imageView = null; Button button0 = null; button button1 = null; @ Override public void onCreate (Bu Ndle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); imageView = (ImageView) findViewById (R. id. imageID); button0 = (Button) findViewById (R. id. btn_01); button1 = (Button) findViewById (R. id. btn_02); button0.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (Intent. ACTION_PICK, null); intent. setDataAnd Type (MediaStore. images. media. EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); startActivityForResult (intent, PHOTOZOOM) ;}}); button1.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); intent. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (new File (Environment. getExternalStorageDirectory (), "temp.jpg"); startAc TivityForResult (intent, PHOTOHRAPH) ;}}) ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {if (resultCode = NONE) return; // take a photo if (requestCode = PHOTOHRAPH) {// set the File storage path to File picture = new File (Environment. getExternalStorageDirectory () + "/temp.jpg"); startPhotoZoom (Uri. fromFile (picture);} if (data = null) return; // read the album zoom image if (requestCod E = PHOTOZOOM) {startPhotoZoom (data. getData ();} // processing result if (requestCode = PHOTORESOULT) {Bundle extras = data. getExtras (); if (extras! = Null) {Bitmap photo = extras. getParcelable ("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream (); photo. compress (Bitmap. compressFormat. JPEG, 75, stream); // (0-100) compresses the imageView file. setImageBitmap (photo);} super. onActivityResult (requestCode, resultCode, data);} public void startPhotoZoom (Uri uri) {Intent intent = new Intent ("com. android. camera. action. CROP "); intent. setDataAndType (uri, IMAGE_UNSPECIFIED); intent. putExtra ("crop", "true"); // aspectX aspectY is a width-high ratio intent. putExtra ("aspectX", 1); intent. putExtra ("aspectY", 1); // outputX outputY is the width and height intent of the cropped image. putExtra ("outputX", 64); intent. putExtra ("outputY", 64); intent. putExtra ("return-data", true); startActivityForResult (intent, PHOTORESOULT );}}

 


Related Article

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.