有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java SetImageUri不显示图像

我想让手机上的图像显示在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/simulated/0/ImageSwaranty/img\u MyWarranty\u ID1。jpg。在手机图像和代码中获取图像的路径上


共 (3) 个答案

  1. # 1 楼答案

    这可能是解决方案的问题。甚至我在显示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);
                            }
    
  2. # 2 楼答案

    当图像的分辨率非常高,系统需要额外几毫秒才能显示时,就会发生这种情况,而且由于这种情况发生在图像视图的UI线程onDraw()中,因此会将其绘制为空。 我发现解决这个问题最简单的方法就是这样使用毕加索图书馆:

    Picasso.get().load(imgUri).fit().centerCrop()
    

    。进入(图像视图)

    这两种方法背后的原因(fit()centerCrop()) 是否适合确保图像适合绑定的容器,然后裁剪图像,这会迫使系统重新绘制图像,但这次使用方便的分辨率(因为适合和裁剪)

  3. # 3 楼答案

    对这个答案的补充 在该旋转中,bug使用ExiFinInterface定义图像的方向

    这是密码

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

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