The ultimate solution for Android big picture cropping

Source: Internet
Author: User

According to our analysis and summary, the photo is from the source of photos and albums, and can take action has

    • Use bitmap and return data
    • Using URIs does not return data

Before we learned that using bitmap may cause the picture to be too large to return the actual size of the picture, I will use the large map URI, small figure bitmap data storage.

We're going to use the URI to save the picture after the photo:

?
12 privatestatic finalString IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp fileUri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap

It is not difficult to know that we choose the image from the album ACTION is Intent.action_get_content.

Based on an analysis of our last blog post, I have prepared two instances of intent.

One, from the album to cut large map:

?
12345678910111213 Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);intent.setType("image/*");intent.putExtra("crop", "true");intent.putExtra("aspectX", 2);intent.putExtra("aspectY", 1);intent.putExtra("outputX", 600);intent.putExtra("outputY", 300);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); // no face detectionstartActivityForResult(intent, CHOOSE_BIG_PICTURE);

Second, from the album to cut small map

?
123456789101112 Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);intent.setType("image/*");intent.putExtra("crop", "true");intent.putExtra("aspectX", 2);intent.putExtra("aspectY", 1);intent.putExtra("outputX", 200);intent.putExtra("outputY", 100);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);

Third, the corresponding Onactivityresult can handle the returned data in this way

?
12345678910111213141516171819 switch (requestCode) {case CHOOSE_BIG_PICTURE:    Log.d(TAG, "CHOOSE_BIG_PICTURE: data = " + data);//it seems to be null    if(imageUri != null){        Bitmap bitmap = decodeUriAsBitmap(imageUri);//decode bitmap        imageView.setImageBitmap(bitmap);    }    break;case CHOOSE_SMALL_PICTURE:    if(data != null){        Bitmap bitmap = data.getParcelableExtra("data");        imageView.setImageBitmap(bitmap);    }else{        Log.e(TAG, "CHOOSE_SMALL_PICTURE: data = " + data);    }    break;default:    break;}
?
12345678910 private Bitmap decodeUriAsBitmap(Uri uri){    Bitmap bitmap = null;    try {        bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));    } catch (FileNotFoundException e) {        e.printStackTrace();        return null;    }    return bitmap;}

Large Small map

Reprint (http://my.oschina.net/ryanhoo/blog/86853)

The ultimate solution for Android big picture cropping

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.