嘿,我似乎无法理解这个错误。我正在尝试通过拍照或从图库中选择来选择图像。当我在选定的图像上尝试该方法时,它工作正常,但是当我从相机拍摄图像时,我得到了cursor.close()
在线错误
我有这段代码可以从图库中捕获图像:
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();
}
}
这会产生空指针异常,我不明白为什么。
有什么帮助吗?
编辑:
这就是我所说的 Intent :
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);
}
}