2017-11-22 12 views
0

を作成していない私は、次のことをしようとしている:保存写真は何のファイル

1. Click a button to take a photo. 
2. Save photo. 
3. Add photo into the gallery. 
4. Show photo into an ImageView. 

14が正常に動作しますが、私は23に問題を抱えています。

これは私のコードです:これにより

photoFile = createImageFile(); 

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = timeStamp; 
    File imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), imageFileName + ".jpg"); 

    mCurrentPhotoPath = imageFile.getAbsolutePath(); 

    return imageFile; 
} 

私は画像を保存する作成したfilePathにしています。

private void galleryAddPic() { 
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    File f = new File(mCurrentPhotoPath); 
    Uri contentUri = Uri.fromFile(f); 
    mediaScanIntent.setData(contentUri); 
    getContext().sendBroadcast(mediaScanIntent); 
} 

別:ギャラリーに写真を追加するonActivityResultが、私は関数を呼び出す私の中で、今

if (photoFile != null) {     
    Uri photoURI = FileProvider.getUriForFile(getContext(), 
        "es.antonio.prueba.fileprovider", 
        photoFile); 
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
    startActivityForResult(takePhotoIntent, REQUEST_CAMERA); 
} 

は今、私は画像を保存するために余分のparamsでIntentを呼び出します写真をImageViewに設定する人:

私の方法がうまくいきません(私はAndroid Developers tutrialに従います)ので、私はSDに撮影した画像を保存するのに苦労しています。私はインテントに余分を渡すことはトリックを行うべきだと思ったが、それは機能していない。

ヒント?

+0

マニフェストのパーミッションは書かれていますか? – Ivan

+0

はい、カメラに追加され、外部記憶装置を読み書きします。 – canillas

+0

'createNewFile();'はtrueまたはfalseを返します。あなたは戻り値をチェックしていません。これを行うためのコードとトーストを追加してユーザーに知らせてください。 – greenapps

答えて

0

が解決:

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = timeStamp; 
    File album = getAlbumStorageDir("MiBodaAPP"); 
    File imageFile = File.createTempFile(imageFileName, ".jpg", album); 

    mCurrentPhotoPath = imageFile.getAbsolutePath(); 

    return imageFile; 
} 

更新onActivityResultそれはFileProvider

更新createImageFile()機能にミスの呼び出しだった。ここ

takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getContext(), "es.antonio.prueba.fileprovider", photoFile)); 

、私は値EXTRA_OUTPUTのparamを取っていました私が環境からの道を得ていたので間違ってFileProviderを起動します。

+0

'File.createTempFile(imageFileName、" .jpg "、アルバム);'へのコールが失敗しました。違う!悪い!既にそのファイルを作成しないでください。ファイル名/パスのみが必要です。元のコードはOKでした。 – greenapps

+0

写真を撮ってこれを作って今作っているAndroid Developers Tutorial ...:S – canillas

+0

はい、私はそれがそのチュートリアルから来ていることを知っています。どのように間違って、悪い彼らはそのようなチュートリアルをすることができます! – greenapps

関連する問題