IMAGE_CAPTURE インテントを使用して写真を撮り、この写真をすぐに画面に表示しようとしています。私がやること:
File photoFile = createImageFile();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(intent, REQUEST_CAMERA);
画像ファイルの作成方法:
private File createImageFile() {
File dir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File photoFile = null;
try {
photoFile = File.createTempFile("photo", ".jpg", dir);
mPhotoPath = photoFile.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
return photoFile;
}
onActivityResultが呼び出されたら、次を実行します。
Bitmap photo = BitmapFactory.decodeFile(mPhotoPath);
mPhoto.setImageBitmap(photo);
ここに問題があります。どうやら IMAGE_CAPTURE アクティビティは画像をバックグラウンドでファイルに保存し、画像が保存される前onActivityResultに実行されます。このため、私の。呼び出しの前にブレークポイントを置いても機能しますが。私は醜いハックでそれを修正しました:photo == nullBitmapFactory.decodeFile
while (photo == null) {
photo = BitmapFactory.decodeFile(mPhotoPath);
}
それで、私は何か間違ったことをしていますか?なぜこのように機能するのですか?
マシュマロ ネクサス 5 でテストしています。