我正在尝试制作一个应用程序,用户可以在其中拍照并将该照片存储在图像视图中。我已经尝试了几十种方法来让它工作。
我看过的很多答案都建议使用 data.getExtra("data") 并使用 MediaStore 意图并传入 ACTION_CAMERA_CAPTURE。这将返回一个较小的图像,并且我尝试显示的图像很大,因此使用此方法图像显示模糊。
另一种选择是使用相同的方法并传入 EXTRA_OUTPUT。但是,这需要放入存储位置,我看到的每个答案都告诉我放入 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)。但是,当我尝试在 onActivityResult 中执行 data.getData() 时,我得到空值。我该怎么做呢?
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);
}
}