SetImageUri显示没有图像

时间:2018-01-20 08:56:21

标签: java android

我想让手机上的图像显示在Imageview中。但是在ANdroid Studio模拟器中它可以工作但不能在我自己的手机上工作。

String imgPath = getIntent().getExtras().getString("imgPath");
    System.out.println(imgPath);


    if(!imgPath.equals("?"))
    {
        File img_file = new File(imgPath);
        ImageView imgView = findViewById(R.id.show_image_war);
        imgView.setImageURI(Uri.fromFile(img_file));
    }

路径为/storage/emulated/0/imagesWarranty/img_MyWarranty_ID1.jpg。我手机中的图像和我的代码中的路径都可以获得图像。

2 个答案:

答案 0 :(得分:4)

这可能是解决问题。当我从uri显示图像时,我甚至得到了分辨率的错误。

我使用下面的代码,它对我有用:

Uri imageUri = Uri.parse(imagePath);
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = true;
                        BitmapFactory.decodeFile(new File(imageUri.getPath()).getAbsolutePath(), options);
                        int imageHeight = options.outHeight;
                        int imageWidth = options.outWidth;
                        if (imageHeight > 4096 || imageWidth > 4096) {
                            BitmapFactory.Options opts = new BitmapFactory.Options();
                            opts.inSampleSize = 4;
                            Bitmap bitmap = BitmapFactory.decodeFile(imageUri.toString(), opts);
                            viewHolder.imgAvatarLogoList3.setImageBitmap(bitmap);
                        } else {
                            Picasso.with(context)
                                    .load(new File(imageUri.getPath())) // Uri of the picture
                                    .into(viewHolder.imgAvatarLogoList3);
                        }

答案 1 :(得分:0)

除了这个答案[https://stackoverflow.com/a/48354307/9255006] 在该旋转错误中,使用ExifInterface定义图像的图像方向。

这是代码

private void SetOrientation(){

    try {
        ExifInterface exif=new ExifInterface(photoURI.getPath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_UNDEFINED);

        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { orientationInDegrees=90; }
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { orientationInDegrees=180; }
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { orientationInDegrees=270; }
        else { orientationInDegrees=0; }

        Toast.makeText(this, String.valueOf(orientationInDegrees), Toast.LENGTH_SHORT).show();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

然后,您可以将此方向设置为图像。

相关问题