【发布时间】:2015-05-30 06:45:18
【问题描述】:
我正在尝试显示相机刚刚拍摄的照片并将其显示在 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.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();
}
}
注释代码是我失败的尝试。
【问题讨论】:
-
你想用上面的代码实现什么???
-
我可以发布我的代码,我会在其中做同样的事情
-
我试过这个:用 android.provider.MediaStore.Images.Media.DATA 改变 MediaStore.Images.Media 没有成功: -(
-
getMediaFileUri 是直接取自样本还是其他东西?
-
这里定义了getoutputmediafileuri()方法:developer.android.com/guide/topics/media/…
标签: android android-camera android-cursor