我正在尝试使用相机 Intent 拍照,但写入存储的图片是空的(大小:0 字节)。有人可以告诉我我的代码有什么问题吗?
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btnImageCapture:
preinsertedUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, OPEN_CAMERA);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case OPEN_CAMERA:
if (resultCode != 0 && data != null) {
Uri imageUri = null;
if (data != null){
imageUri = data.getData();
}
if(imageUri == null && preinsertedUri != null){
imageUri = preinsertedUri;
}
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(imageUri, filePathColumn, null, null, null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
filePath = cursor.getString(columnIndex);
break;
}
}
}