如果用户没有拍照,创建的文件会发生什么?

时间:2017-08-07 04:39:39

标签: android file camera

如果我为用户创建隐含的意图拍摄照片,而他没有(退出,杀死应用程序,取消某些内容),那么我在下面创建的文件会发生什么?

我有以下内容:

String mCurrentPhotoPath;

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 = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

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

static final int REQUEST_TAKE_PHOTO = 1;

private void 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
            ...
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                                                  "com.example.android.fileprovider",
                                                  photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

编辑: 我猜我需要覆盖onActivityResult来检查这个:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        //
        ...
    }else if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode != RESULT_OK){
  //Cleanup here ?
}

}

是吗?如果结果不正确,我应该删除onActivityResult中的文件吗?

1 个答案:

答案 0 :(得分:0)

您不应该创建一个临时文件。您需要创建和提供的唯一内容是文件名。文件路径。在你的情况下是一个文件实例。