我试图显示相机拍摄的照片并将其显示在imageView中。这是有问题的方法:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100 && resultCode == RESULT_OK) {
selectedImage = fileUri;//data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
//managedQuery(selectedImage,filePathColumn, null, null, null);//
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
// The following three lines work perfectly :-)
// if I comment the part of the cursor lines above
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView) findViewById(R.id.Imageprev);
imageView.setImageBitmap(photo);
}
}
调试时我会看到一些值,例如
selectedImage : file:///mnt/sdcard/external_sd/Pictures/MyCameraApp/IMG_20150530_032752.jpg
我认为这没关系。另一个有趣的值是
filePathColumn : _data
这个值是预期值吗?请告诉我。
因此,cursor为null且行
cursor.moveToFirst();
吐出错误空指针: - /。我在使用Android 2.2的真实设备中调试代码。帮助
版
这是调用前一个
的方法private void clickpic() {
// Check Camera
if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
// Open default camera
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
/*
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, 100);
*/
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create
//the getOutputMediaFileUri is implemented as saving media file suggests by developer.android.com
//intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
// start the image capture Intent
startActivityForResult(intent,100);
} else {
Toast.makeText(MainActivity.this, "Camera not supported", Toast.LENGTH_LONG).show();
}
}
评论的代码是我失败的尝试。
答案 0 :(得分:0)
您需要将fileUri
作为额外内容添加,如Android Developers
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
并尝试将onActivityResult
替换为:
if (requestCode == 100 && resultCode == RESULT_OK) {
selectedImage = fileUri;
Bitmap photo = BitmapFactory.decodeFile(new File(selectedImage));
ImageView imageView = (ImageView) findViewById(R.id.Imageprev);
imageView.setImageBitmap(photo);
}