简体   繁体   中英

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:

<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.

First install glide using following steps:

Step 1: In you build.gradle(YourAppName) add these lines

allprojects {
    repositories {
        
        //Other repositories

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

Step 2: In your build.gradle(Module:app) add these lines

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. 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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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