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 |
private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg" ;//temp file Uri 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 detection
startActivityForResult(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 detection
startActivityForResult(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;
}
|
:
Reprint (http://my.oschina.net/ryanhoo/blog/86853)
The ultimate solution for Android big picture cropping