拍照后结果为空
Null result after taking a photo
我需要拍照,获取全尺寸文件以发送到服务器。使用缩略图效果很好,但我无法恢复全尺寸照片。我阅读并复制了 android developers web page 上 google 教程中的大部分代码。
我正在这样做:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
mPhotoFile = null;
try {
mPhotoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (mPhotoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
mCurrentPhotoPath);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp;
File storageDir;
if (StorageUtils.isExternalStorageWritable()) {
storageDir = StorageUtils.getExternalStorageAppDir(getActivity());
} else {
storageDir = Environment.getDataDirectory();
}
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
Bitmap bitmap= BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); // returns null
mImageAdapter.addImage(bitmap);
}
}
这一行(onActivityResult 内 returns null):
Bitmap bitmap= BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
我阅读了很多关于相机问题的帖子,但似乎没有任何效果。我做错了什么?
提前致谢。
注意:我在模拟器和真实设备中测试了代码。结果相同。
问题出在这一行:
bmOptions.inJustDecodeBounds = true;
关于 bmOptions.inJustDecodeBounds 的 google 文档说:
If set to true, the decoder will return null (no bitmap), but the
out... fields will still be set, allowing the caller to query the
bitmap without having to allocate the memory for its pixels.
删除此行后,图像返回正常。
我需要拍照,获取全尺寸文件以发送到服务器。使用缩略图效果很好,但我无法恢复全尺寸照片。我阅读并复制了 android developers web page 上 google 教程中的大部分代码。
我正在这样做:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
mPhotoFile = null;
try {
mPhotoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (mPhotoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
mCurrentPhotoPath);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp;
File storageDir;
if (StorageUtils.isExternalStorageWritable()) {
storageDir = StorageUtils.getExternalStorageAppDir(getActivity());
} else {
storageDir = Environment.getDataDirectory();
}
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
Bitmap bitmap= BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); // returns null
mImageAdapter.addImage(bitmap);
}
}
这一行(onActivityResult 内 returns null):
Bitmap bitmap= BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
我阅读了很多关于相机问题的帖子,但似乎没有任何效果。我做错了什么?
提前致谢。
注意:我在模拟器和真实设备中测试了代码。结果相同。
问题出在这一行:
bmOptions.inJustDecodeBounds = true;
关于 bmOptions.inJustDecodeBounds 的 google 文档说:
If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
删除此行后,图像返回正常。