将图像缩放为缩略图大小并加载到图像视图中以节省内存的问题

发布于 2024-11-16 23:30:08 字数 854 浏览 3 评论 0原文

我试图将图像缩小到更小的尺寸,我将它们加载到图像视图中,然后我开始收到“位图大小超出虚拟机预算”异常。另一方面,如果我获得缩略图而不是实际图像并向它们显示,则运行会很顺利。但我需要实际图像的 URI 供以后使用,如果我立即加载缩略图,则无法获得这些 URI。

查了一下,找到了一个办法,但是不行。

代码:

Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, name);
uriList.add(uri.getPath());

File image = new File(uri.getPath());

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap actualBitmap = BitmapFactory.decodeFile(image.getPath(), options);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(actualBitmap, image_width, image_height, false);
iv.setImageBitmap(scaledBitmap);

我获取actualBitmap的部分返回null,因此imageview是空的。如果我打印 uri.getpath() ,它会给出:

/external/images/media//sdcard/dcim/Camera/imagename.jpg

我的问题是,这是正确的方法吗?如果是,我做错了什么,如果不是,请有人指出我正确的方向。

I am trying to scale my images down to smaller size I was loading them as they were into an imageview and I started getting a "bitmap size exceeds VM budget" exception. On the other hand if I get the thumbnails instead of the actual images and show them it runs smooth. But I need the URIs of the actual images for later use which I can't get if I straight away load the thumbnails.

After looking around, I found a way, but it's not working.

Code:

Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, name);
uriList.add(uri.getPath());

File image = new File(uri.getPath());

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap actualBitmap = BitmapFactory.decodeFile(image.getPath(), options);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(actualBitmap, image_width, image_height, false);
iv.setImageBitmap(scaledBitmap);

The part where I get the actualBitmap returns null, and so the imageview is empty. If I print uri.getpath() it gives:

/external/images/media//sdcard/dcim/Camera/imagename.jpg

My question is, is this the right approach? If it is, what am I doing wrong and if it's not can someone please point me in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2024-11-23 23:30:08

尝试使用以下代码

public static Bitmap resizeBitMapImage1(String filePath, int targetWidth,
            int targetHeight) {
        Bitmap bitMapImage = null;
        // First, get the dimensions of the image
        Options options = new Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        double sampleSize = 0;
        // Only scale if we need to
        // (16384 buffer for img processing)
        Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
                .abs(options.outWidth - targetWidth);

        if (options.outHeight * options.outWidth * 2 >= 1638) {
            // Load, scaling to smallest power of 2 that'll get it <= desired
            // dimensions
            sampleSize = scaleByHeight ? options.outHeight / targetHeight
                    : options.outWidth / targetWidth;
            sampleSize = (int) Math.pow(2d,
                    Math.floor(Math.log(sampleSize) / Math.log(2d)));
        }

        // Do the actual decoding
        options.inJustDecodeBounds = false;
        options.inTempStorage = new byte[128];
        while (true) {
            try {
                options.inSampleSize = (int) sampleSize;
                bitMapImage = BitmapFactory.decodeFile(filePath, options);

                break;
            } catch (Exception ex) {
                try {
                    sampleSize = sampleSize * 2;
                } catch (Exception ex1) {

                }
            }
        }

        return bitMapImage;
    }

try with the following code

public static Bitmap resizeBitMapImage1(String filePath, int targetWidth,
            int targetHeight) {
        Bitmap bitMapImage = null;
        // First, get the dimensions of the image
        Options options = new Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        double sampleSize = 0;
        // Only scale if we need to
        // (16384 buffer for img processing)
        Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
                .abs(options.outWidth - targetWidth);

        if (options.outHeight * options.outWidth * 2 >= 1638) {
            // Load, scaling to smallest power of 2 that'll get it <= desired
            // dimensions
            sampleSize = scaleByHeight ? options.outHeight / targetHeight
                    : options.outWidth / targetWidth;
            sampleSize = (int) Math.pow(2d,
                    Math.floor(Math.log(sampleSize) / Math.log(2d)));
        }

        // Do the actual decoding
        options.inJustDecodeBounds = false;
        options.inTempStorage = new byte[128];
        while (true) {
            try {
                options.inSampleSize = (int) sampleSize;
                bitMapImage = BitmapFactory.decodeFile(filePath, options);

                break;
            } catch (Exception ex) {
                try {
                    sampleSize = sampleSize * 2;
                } catch (Exception ex1) {

                }
            }
        }

        return bitMapImage;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文