利用来自意图MediaStore.EXTRA_OUTPUT的数据

时间:2014-11-30 00:22:50

标签: android io uri

我一直试图让我的Uri数据从相机功能中继续存在,但我没有运气。我在stackoverflow上使用了几个例子,但仍然不明白我做错了什么。 onActivityResult中的intent数据为null,因为相机活动正在破坏我正在使用的MainActivity。我试图将它保存在SaveInstanceState中,但我仍然是null。我错过了什么?

这是saveInstanceState:

    @Override
protected void onSaveInstanceState(Bundle state){

    super.onSaveInstanceState(state);
    if (mImageData != null) {
        state.putString("cameraImageUri", mImageData.toString());
    }
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState.containsKey("cameraImageUri")) {
        mImageData = Uri.parse(savedInstanceState.getString("cameraImageUri"));
    }
}

以下是意图设置和文件创建者:

    private void dispatchTakePictureIntent() {

    log("in dispatchTakePictureIntent()");
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent

    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            log("Exception caught.  Aborting image creation" + ex);
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            mImageData = takePictureIntent.getData();  //Trying to save the data.
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    log("in onActivityResult()");
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == 1 && resultCode == RESULT_OK){

        //Intent data is null right here.
        Uri selectedImage = data.getData();
        ImageView photo = (ImageView) findViewById(R.id.imageView1);
        Bitmap mBitmap = null;

        try
        {
            mBitmap = Media.getBitmap(this.getContentResolver(), selectedImage);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        SelfieObject selfie = new SelfieObject(mCurrentPhotoPath, photo);
        startNotification();

        //method call for ListViewAdapter.add
        //addNewPic(selfie);
    }
}

private File createImageFile() throws IOException {

    log("in createImageFile()");
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

public void addNewPic(SelfieObject selfie){

    log("in addNewPic()");

    mAdapter.add(selfie);
}

1 个答案:

答案 0 :(得分:0)

我不知道这是否仍然与你有关,但我会试一试。

目前还不清楚你要做什么。当您有选择图像的请求时,所选图像的uri将在Intent数据中可用。如果您要求使用相机拍摄照片,则由您决定保存照片的位置。它代码中的这一行:

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));

因此,在这种情况下,您的Intent数据中始终会为空,因为您不需要它:照片会保存在任何Uri photoFile中。

你显然不需要这一行:

mImageData = takePictureIntent.getData();  //Trying to save the data

...而且还没有必要在onSaveInstanceState中保留它。

我希望它有所帮助。祝你好运!