拍照后的结果为空

时间:2016-05-05 23:19:41

标签: android android-camera android-camera-intent

我需要拍照,获取全尺寸文件发送到服务器。使用缩略图,它工作正常,但我无法恢复全尺寸照片。我在android developers web page上阅读并复制了谷歌教程中的大部分代码。

我这样做:

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内部返回null):

Bitmap bitmap= BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

我阅读了很多关于相机问题的帖子,但似乎没什么用。我做错了什么?

提前致谢。

注意:我在模拟器和真实设备中测试代码。结果相同。

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

bmOptions.inJustDecodeBounds = true;

关于bmOptions.inJustDecodeBounds的google文档说:

  

如果设置为true,解码器将返回null(无位图),但是   out ...字段仍将设置,允许调用者查询   位图,而不必为其像素分配内存。

删除此行后,图像返回确定。