MediaStore.EXTRA_OUTPUT数据呈现空,其他的方式来保存的照片? [英] MediaStore.EXTRA_OUTPUT renders data null, other way to save photos?

查看:505
本文介绍了MediaStore.EXTRA_OUTPUT数据呈现空,其他的方式来保存的照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谷歌提供的这种多功能的code通过一个意图拍摄照片:

Google offers this versatile code for taking photos via an intent:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

现在的问题是,如果你和我一样,你想通过照片作为群众演员,用 EXTRA_OUTPUT 看似有照片数据逃跑,使后续行动想意图的数据为空。

The problem is that if you're like me and you want to pass the photos as extras, using EXTRA_OUTPUT seemingly runs off with the photo data and makes subsequent actions think the intent data is null.

看来这是与Android一大错误。

It appears this is a big bug with Android.

我想拍摄照片​​,然后将其显示为一个新的视图中的缩略图。我希望把它保存在用户的画廊一个全尺寸的图像。有谁知道一种方法,而无需使用指定图像的位置 EXTRA_OUTPUT

I'm trying to take a photo then display it as a thumbnail in a new view. I'd like to have it saved as a full sized image in the user's gallery. Does anyone know a way to specify the image location without using EXTRA_OUTPUT?

下面是我目前有:

public void takePhoto(View view) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
//  takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
}

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
@SuppressLint("SimpleDateFormat")
private static File getOutputMediaFile(int type){

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "JoshuaTree");

    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("JoshuaTree", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            handleSmallCameraPhoto(data);
        }
    }
}


private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mImageBitmap = (Bitmap) extras.get("data");
    Intent displayIntent = new Intent(this, DisplayPhotoActivity.class);
    displayIntent.putExtra("BitmapImage", mImageBitmap);
    startActivity(displayIntent);
}

}

推荐答案

如果您指定MediaStore.EXTRA_OUTPUT,拍摄的图像将被写入到该路径,并且没有数据将提供给onActivityResult。你可以阅读从您所指定的图像。

If you specified MediaStore.EXTRA_OUTPUT, the image taken will be written to that path, and no data will given to onActivityResult. You can read the image from what you specified.

在这里见另一个解决同一个问题: Android摄像头:数据意图返回null

See another solved same question here: Android Camera : data intent returns null

这篇关于MediaStore.EXTRA_OUTPUT数据呈现空,其他的方式来保存的照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆