以下的代码可以调用系统的拍照程序,
1
2
intent it = newintent("android.media.action.image_capture");
startactivityforresult(it, activity.default_keys_dialer);
按下拍照键后,会返回到你的activity,所以你的activity要在onactivityresult方法里加一个处理,
3
4
5
6
7
8
9
10
11
12
13
protectedvoidonactivityresult(intrequestcode, intresultcode, intent data) {
super.onactivityresult(requestcode, resultcode, data);
try{
bundle extras = data.getextras();
bitmap b = (bitmap) extras.get("data");
take = b;
imageview img = (imageview)findviewbyid(r.id.image);
img.setimagebitmap(take);
}catch(exception e){
}
}
但是这样你会发现这个bitmap尺寸太小了。明显是被压缩过了,要像返回未被压缩的照片,那么你要给调用系统拍照程序intent加上参数,指定图片输出的位置。
it.putextra(mediastore.extra_output, uri.fromfile(newfile(f.sd_card_temp_photo_path)));
这样就是大图片返回了。
take = u.resizebitmap(u.getbitmapforfile(f.sd_card_temp_photo_path), 640);
imgflag = true;
另外注意一下,返回的那个bitmap会很大,你用完以后要把它回收掉,不然你很容易内存报oom错误
14
15
16
17
publicstaticbitmap resizebitmap(bitmap bitmap, intnewwidth) {
intwidth = bitmap.getwidth();
intheight = bitmap.getheight();
floattemp = ((float) height) / ((float) width);
intnewheight = (int) ((newwidth) * temp);
floatscalewidth = ((float) newwidth) / width;
floatscaleheight = ((float) newheight) / height;
matrix matrix = newmatrix();
// resize the bit map
matrix.postscale(scalewidth, scaleheight);
// matrix.postrotate(45);
bitmap resizedbitmap = bitmap.createbitmap(bitmap, 0, 0, width, height, matrix, true);
bitmap.recycle();
returnresizedbitmap;