当我用相机拍照然后我想在ImageView中显示这个图像时,我正在按照下一个方法:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {
Bitmap srcBmp = (Bitmap) data.getExtras().get("data");
... (process image to scale size and rotate if necesary)
pic_view.setImageBitmap(srcBmp);
}
}
}
我正在获取图像并正确显示在ImageView中,但我意识到所获得的图像质量非常低。经过一些研究,我发现用这种方法获得的图像是拍摄图像的缩略图。所以我根据其他SO帖子的一些说明修改了代码:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "Pictures/timeStamp.jpg";
takenPicUri = Uri.fromFile(new File(imageFilePath));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, takenPicUri);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {
Bitmap srcBmp = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, null);
... (process image to scale size and rotate if necesary)
pic_view.setImageBitmap(srcBmp);
}
}
}
但是现在,图像没有显示在ImageView(pic_view)中。在其他帖子中,我读过人们报告说这种方法适用于他们,但对我不起作用。我忘了什么或者我做错了什么?
答案 0 :(得分:2)
好吧,我在旧帖子中读过,这样做可能会造成麻烦,因为有一些与之相关的错误,但这是我实现它的唯一方法。
就像这样简单:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {
Uri selectedImageUri = data.getData();
Bitmap srcBmp = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, null);
... (process image to scale size and rotate if necesary)
pic_view.setImageBitmap(srcBmp);
}
}
}
这种方式适用于android 5.0和4.4.4。