根據咱們的分析與總結,圖片的來源有拍照和相冊,而可採起的操做有java
前面咱們瞭解到,使用Bitmap有可能會致使圖片過大,而不能返回實際大小的圖片,我將採用大圖Uri,小圖Bitmap的數據存儲方式。spa
咱們將要使用到URI來保存拍照後的圖片:.net
1
2
|
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
|
不難知道,咱們從相冊選取圖片的Action爲Intent.ACTION_GET_CONTENT。code
根據咱們上一篇博客的分析,我準備好了兩個實例的Intent。orm
1、從相冊截大圖:blog
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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);
|
2、從相冊截小圖圖片
1
2
3
4
5
6
7
8
9
10
11
12
|
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);
|
3、對應的onActivityResult能夠這樣處理返回的數據ci
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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
;
}
|
1
2
3
4
5
6
7
8
9
10
|
private
Bitmap decodeUriAsBitmap(Uri uri){
Bitmap bitmap =
null
;
try
{
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
}
catch
(FileNotFoundException e) {
e.printStackTrace();
return
null
;
}
return
bitmap;
}
|
效果圖:get
大圖 | 小圖 |
轉載(http://my.oschina.net/ryanhoo/blog/86853)博客