简体   繁体   English

如何在 Android Studio 中显示高分辨率的图像?

[英]How to show images with a high resolution in Android Studio?

I am trying to show the selected image from gallery to my Image View.我正在尝试将所选图像从画廊显示到我的图像视图。 But it only shows the images with low pixels on selecting images with high pixels it is showing following error.但它只显示低像素的图像,选择高像素的图像时会显示以下错误。

W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (3120x4160, max=4096x4096)
W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (3120x4160, max=4096x4096)
     Bitmap too large to be uploaded into a texture (3120x4160, max=4096x4096)

How can I automatically adjust the size/resolution of the selected image to fit according to devices limit.如何根据设备限制自动调整所选图像的大小/分辨率以适合。

Here is my code:这是我的代码:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 1 && resultCode == RESULT_OK && data != null){
            Uri selectedImage = data.getData();
            try {
                Bitmap bitmap = null;
                if (android.os.Build.VERSION.SDK_INT >= 29) {
                    ImageDecoder.Source source = ImageDecoder.createSource(this.getContentResolver(), selectedImage);
                    bitmap = ImageDecoder.decodeBitmap(source);
                }
                else{
                    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
                }
                

                ImageView imageView = findViewById(R.id.imageView);
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Here is my xml code for Image View:这是我的 xml 图像视图代码:

<ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_foreground" />

I found a solution using glide.我找到了一个使用 glide 的解决方案。

First install glide using following steps:首先使用以下步骤安装 glide:

Step 1: In you build.gradle(YourAppName) add these lines第 1 步:在你的 build.gradle(YourAppName) 中添加这些行

allprojects {
    repositories {
        
        //Other repositories

        mavenCentral()
        maven { url 'https://maven.google.com' }
    }
}

Step 2: In your build.gradle(Module:app) add these lines第 2 步:在您的 build.gradle(Module:app) 中添加这些行

dependencies {
   
    // other dependencies

    implementation 'com.github.bumptech.glide:glide:4.11.0'
}

Now sync your project.现在同步您的项目。 Then you only need to get Uri of the image.那么你只需要获取图片的Uri即可。 Adjusted code:调整后的代码:


 @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 1 && resultCode == RESULT_OK && data != null){
            Uri selectedImage = data.getData();           

                ImageView imageView = findViewById(R.id.imageView);
                try{
 
                Glide.with(this)
                        .load(selectedImage)
                        .into(imageView);

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

In XML add this line:在 XML 添加这一行:

android:scaleType="fitCenter"

Your error message says it all, the size of your image is too large.您的错误消息说明了一切,您的图像尺寸太大。 Here's something to resize it:这里有一些调整它的大小:

public Bitmap resizeBitmap(Bitmap source, int width,int height) {
   if(source.getHeight() == height && source.getWidth() == width) return source;
   int maxLength=Math.min(width,height);
   try {
      source=source.copy(source.getConfig(),true);
      if (source.getHeight() <= source.getWidth()) {
        if (source.getHeight() <= maxLength) { // if image already smaller than the required height
            return source;
        }

        double aspectRatio = (double) source.getWidth() / (double) source.getHeight();
        int targetWidth = (int) (maxLength * aspectRatio);

        return Bitmap.createScaledBitmap(source, targetWidth, maxLength, false);
     } else {

        if (source.getWidth() <= maxLength) { // if image already smaller than the required height
            return source;
        }

        double aspectRatio = ((double) source.getHeight()) / ((double) source.getWidth());
        int targetHeight = (int) (maxLength * aspectRatio);

        return Bitmap.createScaledBitmap(source, maxLength, targetHeight, false);

       }
   }
   catch (Exception e)
   {
    return source;
   }
}

A solution I came up with to work with large bitmaps was this.我想出的一个处理大位图的解决方案是这样的。 The way this works is your code will call the second method, which will call the first method, and then you've got a bitmap that'll be scaled for your device.它的工作方式是您的代码将调用第二个方法,该方法将调用第一个方法,然后您将获得一个 bitmap,它将针对您的设备进行缩放。

    public static Bitmap getScaledBitmap(String path, int destWidth, int destHeight)
    {
        // read in the dimensions of the image on disk
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        float srcWidth = options.outWidth;
        float srcHeight = options.outHeight;

        int inSampleSize = 1;
        if (srcHeight > destHeight || srcWidth > destWidth)
        {
            if (srcWidth > srcHeight)
            {
                inSampleSize = Math.round(srcHeight / destHeight);
            } else
            {
                inSampleSize = Math.round(srcWidth / destWidth);
            }
        }

        options = new BitmapFactory.Options();
        options.inSampleSize = inSampleSize;

        return BitmapFactory.decodeFile(path, options);
    }

   public static Bitmap getScaledBitmap(String path, Activity activity)
    {
        android.graphics.Point size = new android.graphics.Point();
        activity.getWindowManager().getDefaultDisplay().getSize(size);

        return getScaledBitmap(path, size.x, size.y);
    }

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

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