调用安卓手机系统照相机进行拍照
系统照相机得到的相片分辨率太大,如果不处理直接使用就会导致OutOfMemery异常,我采用的是下面这段代码:
// 计算图片的缩放值
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
// 根据路径获得图片并压缩,返回bitmap用于显示
public static Bitmap getSmallBitmap(String filePath, int width, int height) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
if(width < height){
options.inSampleSize = calculateInSampleSize(options, width, height);
}else{
options.inSampleSize = calculateInSampleSize(options, height, width);
}
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
这段代码可以按照View的大小对图片进行缩放,当一个Bitmap 不使用时,一定要使用recycle进行释放。
今天在调用照相机时还出现了一个onActivityR esult无法执行的问题,网上说是因为传入文件URI路径的权限问题,具体我也没搞懂..蛋疼
现在的问题是横屏拍摄缩放问题,照相机横屏拍摄时缩放方式不正确。