简体   繁体   English

SetImageUri显示无图像

[英]SetImageUri showing no Image

I want to let a image on my phone to show up in the Imageview. 我想让手机上的图像显示在Imageview中。 But in the ANdroid Studio emulator it is working but not on my own phone. 但是在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));
    }

The path is /storage/emulated/0/imagesWarranty/img_MyWarranty_ID1.jpg . 路径为/storage/emulated/0/imagesWarranty/img_MyWarranty_ID1.jpg。 Both on the image in my phone and the path in my code where I get the image. 在手机中的图像上以及在代码中获取图像的路径上。

It might be issue of resolution. 这可能是解决问题。 Even I was getting error of resolution while I was displaying image from uri. 从uri显示图像时,甚至出现分辨率错误。

I used below code and It worked for me : 我使用下面的代码,它为我工作:

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);
                        }

Addition to this answer [ https://stackoverflow.com/a/48354307/9255006] In that rotation bug define the image orientation for the image using ExifInterface. 此答案的附加内容[ https://stackoverflow.com/a/48354307/9255006]在该旋转错误中,使用ExifInterface定义了图像的图像方向。

Here is the code 这是代码

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();
    }
}

You can then set this orientation to your image. 然后,您可以将此方向设置为图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM