2017-08-07 10 views
0

ユーザーが写真を撮るという暗黙的な意図を作成し、彼が(終了して、アプリケーションを終了し、何らかの方法をキャンセルした)場合、以下で作成したファイルはどうなりますか?ユーザーが写真を撮っていないと作成されるファイルはどうなりますか?

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); 
     } 
    } 
} 

EDIT: は私がこれをチェックする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 ? 
} 

}

はそれが右か?結果がOKでない場合は、onActivityResultでファイルを削除する必要がありますか?

答えて

0

まず、一時ファイルを作成しないでください。作成して提供する必要があるのはファイル名だけです。ファイルパス。あなたの場合、Fileインスタンス。

+0

理由を説明できますか?私が貼り付けたコードは公式の例からです:https://developer.android.com/training/camera/photobasics.html –

+0

はい私はその公式の例を知っています。そして非常に悪いコード。あなたはすでにその理由を発見しました。 – greenapps

+0

ファイルを削除するだけで済みますか?私が実際にやるべきことのコードサンプルがありますか? –

関連する問題