我正在通过相机拍摄照片,质量很低或有时候中等,相机返回正确uri
并将照片添加到我的应用中。
当我在三星手机中设置我的相机高质量而不返回图片时,在HTC手机中给我一个错误nullpointerexception: uri
(或在其他手机中崩溃我的应用程序)但质量低劣的工作完美无缺。
我的代码:
imCamera.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 100);
}
show.dismiss();
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
...
try {
Bitmap thumbnail = MediaStore.Images.Media.getBitmap(
getContentResolver(), imageUri);
String photoPath = getRealPathFromURI(imageUri);
ExifInterface ei = new ExifInterface(photoPath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap rotatedBitmap = null;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(thumbnail, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(thumbnail, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(thumbnail, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
rotatedBitmap = thumbnail;
}
if (rotatedBitmap != null) {
ivImage.setImageBitmap(rotatedBitmap);
File tempFile = GetTempFile(rotatedBitmap);
if (tempFile != null)
crImage = tempFile;
}
} catch (Exception e) {
e.printStackTrace();
}
更新:
我在android上测试了它:4,5,6,7
但是当质量很高时,它仍然会给我错误。
更新: 也许是因为我在主线程中这样做了?