ユーザーが写真を撮ることができ、その写真がイメージビューに保存されるアプリを作成しようとしています。これを機能させるために、何十もの方法を試しました。
私が見た多くの回答は、MediaStore インテントを使用して ACTION_CAMERA_CAPTURE を渡すことで data.getExtra("data") を使用することを提案しています。これは小さい画像を返し、表示しようとしている画像が大きいため、この方法を使用すると画像がぼやけて表示されます。
もう 1 つのオプションは、これと同じ方法を使用して EXTRA_OUTPUT を渡すことです。ただし、これにはストレージの場所を配置する必要があり、私が見たすべての回答は、Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) に配置するように指示しています。ただし、onActivityResult で data.getData() を実行しようとすると、null になります。どうすればいいですか?
alert.setPositiveButton("Take Photo", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
profileFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "image_" +
String.valueOf(System.currentTimeMillis()) + ".jpg");
profile = Uri.fromFile(profileFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, profile);
startActivityForResult(intent, 1);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
ImageView imageView = (ImageView) findViewById(R.id.ProfileImage);
//This returns null.
//I am guessing that the Environment.getExternalSt...() method is getting a storage
//location that does not exist.
//I don't know what the correct storage location is and I have not
//been able to find it.
Uri u = data.getData();
File finalFile = new File(getRealPathFromURI(u));
Picasso.with(this).load(finalFile).into(imageView);
}
}