我有一个问题,当我与相机在一些设备和版本的安卓。例如,在我的xperia u v4.0.3中,我的代码运行良好,但在另一个xperia u v2.3.3中,它不工作…
我读了很多关于这个错误的书,但是我没能纠正它…
我的拍照和展示代码:
public void callCamera(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, SELECT_CAMERA);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_CAMERA) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
try {
imageSelected = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
// I tried to read the image created for the camera in the Sd
//But I've got the same error...
//imageSelected = Utils.readImageSD(this, selectedImage.getPath());
imageSelected = Utils.rotateImage(Utils.scaleBitmap(imageSelected, 160, 160));
ivImageLoad.setImageBitmap(imageSelected);
} catch (Exception e) {
Utils.showToast(getApplicationContext(), R.string.errorLoad);
Log.e("Camera", e.toString());
}
}
}
我希望有人能帮我…我不知道我能做什么…
提前谢谢。
当做。
最佳答案
看起来您只想显示一个160x160位图,但在这一行上,您正在读取整个位图,然后将其缩小。
imageSelected = android.provider.MediaStore.Images.Media(cr, selectedImage);
使用inJustDecodeBounds可以避免将整个位图加载到内存中,然后使用inSampleSize可以缩放它。
下面是Loading Large Bitmaps Efficiently上android系列的一些示例代码
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromUri(Uri uri,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
}
然后要获得160x160图像,您只需执行以下操作:
ivImageLoad.setImageBitmap(decodeSampledBitmapFromUri(selectedImage, 100, 100));