0
嘿,我似乎无法理解这个错误。 我试图通过拍照或从图库中选择图像。 当我尝试的方法选择的图像,它工作正常,但是当我从相机拍摄的图像我上cursor.close()
行错误的NullPointerException当试图获取图像方向
我有这样的代码从库捕捉图像:
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
Uri selectedImage = mImageUri;
getContentResolver().notifyChange(selectedImage, null);
ImageView imageView = (ImageView) findViewById(R.id.chosenImage2);
ContentResolver cr = getContentResolver();
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
//flip image if needed
bitmap = Helpers.flipBitmap(bitmap, Helpers.getOrientation(this, selectedImage));
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
Log.e("Camera", e.toString());
}
}
这是getOrientation代码:
public static int getOrientation(Context context, Uri photoUri) {
Cursor cursor = context.getContentResolver().query(photoUri,
new String[] { MediaStore.Images.ImageColumns.ORIENTATION },
null, null, null);
try {
if (cursor.moveToFirst()) {
return cursor.getInt(0);
} else {
return -1;
}
} finally {
cursor.close();
}
}
这生成空指针异常,我不明白为什么。
任何帮助?
编辑:
这是我所说的意图:
ImageView imageView = (ImageView) findViewById(R.id.chosenImage2);
if(imageView.getDrawable() == null){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+ ".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
mImageUri = Uri.fromFile(photo);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
除此之外,它很可能是零,因为你的URI参数是无效的。一个可能的原因是你没有恢复活动状态,并且mImageUri领域成为开启相机后空。 –
这会使错误消失,但它不会再获取图像方向。和Paul-Jan,我不明白转动相机的方式是如何相关的,我只是提出了活动结果,并且只做了活动肖像,以至于不能成为原因。 –
对不起,对于迟到的答复的方式:)经过适当的错误处理是时间来质疑为什么错误发生 - 阅读:为什么'游标'为空? Aus Paul-Jan指出机会很高'photoUri'无效,因此'光标'的创建失败。看看第一个代码块,我看到这个moethod就像'Helpers.getOrientation(this,selectedImage)'一样被调用,而基本上'selectedImage = mImageUri'。后者“从代码天堂下降”。如果你不能从这里解决它,我会建议打开另一个更具体的问题。 – yoshi