【问题标题】:Scaling or proportionally resizing image picked from the gallery缩放或按比例调整从图库中挑选的图像
【发布时间】:2017-03-26 03:51:43
【问题描述】:

在我的应用程序中,我需要从图库中选择图像到我的 ImageView。如果我选择简单的图像,例如从 Internet 下载,它可以正常工作。但如果我选择用手机拍摄的照片,我会得到W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (4160x3120, max=4096x4096)

使用 Glide 加载照片会得到这个

Caused by: java.lang.IllegalArgumentException: Unknown type class android.graphics.Bitmap. You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class

这是我的代码,它拾取图像并从中创建位图。

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

    // When an Image is picked
    if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK
            && null != data) {

        Uri imageUri = data.getData();
        InputStream inputStream;
        try {
            inputStream = getContentResolver().openInputStream(imageUri);
            Bitmap image = BitmapFactory.decodeStream(inputStream);
            commentImage = (ImageView) findViewById(R.id.comment_image);
            //Glide.with(this).load(image).asBitmap().override(4095, 4095).into(commentImage);
            commentImage.setImageBitmap(image);
            //commentImage.setImageBitmap(Bitmap.createScaledBitmap(image, 4095, 2048, false));
            Toast.makeText(this, "SUCCESS", Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            // show a message to the user indictating that the image is unavailable.
            Toast.makeText(this, "Unable to open image", Toast.LENGTH_LONG).show();
        }

    } else {
        Toast.makeText(this, "You haven't picked Image",
                Toast.LENGTH_LONG).show();
    }
}

如果图像太大,我该如何缩放?

【问题讨论】:

    标签: java android bitmap


    【解决方案1】:

    您可以使用以下压缩位图,这里的 bitmapImage 是您的位图对象。 :

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmapImage.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);  //90 refers to 90% quality, or 10% compression
            byte[] data = byteArrayOutputStream.toByteArray();
    
    Bitmap compressedBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    

    【讨论】:

    • 不幸的是,它仍然显示W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (4160x3120, max=4096x4096)
    • @TheAlcatras21 :看起来图像真的很大,让我们尝试使用 80 的质量(20% 压缩)。正如错误所说,压缩后分辨率为 4160x3120,最大可能为 4096x4096。
    猜你喜欢
    • 1970-01-01
    • 2012-08-29
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    相关资源
    最近更新 更多