繁体   English   中英

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

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

我正在尝试将所选图像从画廊显示到我的图像视图。 但它只显示低像素的图像,选择高像素的图像时会显示以下错误。

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)

如何根据设备限制自动调整所选图像的大小/分辨率以适合。

这是我的代码:

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

这是我的 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" />

我找到了一个使用 glide 的解决方案。

首先使用以下步骤安装 glide:

第 1 步:在你的 build.gradle(YourAppName) 中添加这些行

allprojects {
    repositories {
        
        //Other repositories

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

第 2 步:在您的 build.gradle(Module:app) 中添加这些行

dependencies {
   
    // other dependencies

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

现在同步您的项目。 那么你只需要获取图片的Uri即可。 调整后的代码:


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

在 XML 添加这一行:

android:scaleType="fitCenter"

您的错误消息说明了一切,您的图像尺寸太大。 这里有一些调整它的大小:

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

我想出的一个处理大位图的解决方案是这样的。 它的工作方式是您的代码将调用第二个方法,该方法将调用第一个方法,然后您将获得一个 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