在imageview上显示标签时发生内存不足 [英] Out of memory happened on showing sticker on imageview

查看:84
本文介绍了在imageview上显示标签时发生内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图像和贴纸(webp格式)的列表必须显示在回收视图中.

a list of images and stickers(webp format) must be shown on a recycleview.

要在imageView上显示标签,此[存储库]( https://github.com/EverythingMe/webp-android ).此存储库是建议的存储库之一 这篇文章的解决方案( WebP for Android )

to show sticker on imageView, this [repository] (https://github.com/EverythingMe/webp-android) is used. this repository was one of suggested solution on this post(WebP for Android)

sticker文件,使用存储库将其转换为字节数组,将字节数组转换为位图,最后在imageView上显示位图.下面的代码将贴纸文件转换为位图

sticker file is readed from external storage, convert to byte array, by using library of the repository, byte array convert to bitmap, and finally bitmap is shown on imageView. below code convert sticker file to bitmap

private void ShowStickerOnImageView(String stickerPath){
File file = new File(stickerPath);
int size = (int) file.length();
byte[] bytes = new byte[size];

BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();

Bitmap bitmap = null;
boolean NATIVE_WEB_P_SUPPORT = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
if (!NATIVE_WEB_P_SUPPORT) {
    bitmap = WebPDecoder.getInstance().decodeWebP(bytes);
} else {
    bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}

holder.imageView.setImageBitmap(bitmap);
}

.....

public Bitmap decodeWebP(byte[] encoded, int w, int h) {
int[] width = new int[]{w};
int[] height = new int[]{h};

byte[] decoded = decodeRGBAnative(encoded, encoded.length, width, height);
if (decoded.length == 0) return null;

int[] pixels = new int[decoded.length / 4];
ByteBuffer.wrap(decoded).asIntBuffer().get(pixels);

return Bitmap.createBitmap(pixels, width[0], height[0], Bitmap.Config.ARGB_8888);
}

当'NATIVE_WEB_P_SUPPORT'为假时,调用'decodeWebP'方法,该方法在大多数时间都能正常工作,但有时此方法会发生内存不足"错误.大多数情况下,此错误发生在这些行上

when 'NATIVE_WEB_P_SUPPORT' is false, 'decodeWebP' method is called, this method work fine in most of the time, but sometimes 'out of memory' error is happened on this method. most of the time, this error is happened on these lines

int[] pixels = new int[decoded.length / 4];
ByteBuffer.wrap(decoded).asIntBuffer().get(pixels);
return Bitmap.createBitmap(pixels, width[0], height[0], Bitmap.Config.ARGB_8888);

我发现贴纸文件的字节数组长度很大,可以通过编程方式减小贴纸文件的大小吗?我想找到解决方案,以减少字节数组的大小.

i found that byte array length of sticker file is big , can i decrease sticker file size programmatically? i want to find solution, to decrease byte array size.

推荐答案

您正在创建一个Bitmap,它被用作本机大小,但应用于了ImageView.将Bitmap减小为View的大小:

You are creating a Bitmap that is being used as native size, but applied to an ImageView. Decrease the Bitmap to the size of the View:

Bitmap yourThumbnail= Bitmap.createScaledBitmap(
   theOriginalBitmap,
   desiredWidth,
   desiredHeight,
   false
);

请注意:

public static Bitmap createBitmap(int colors[], int width, int height, Config config) {
    return createBitmap(null, colors, 0, width, width, height, config);
}

会打电话给

public static Bitmap createBitmap(DisplayMetrics display, int colors[],
        int offset, int stride, int width, int height, Config config)

这将导致:

Bitmap bm = nativeCreate(colors, offset, stride, width, height,
                        config.nativeInt, false);

基本上,您不能无缘无故地在内存中创建巨大的Bitmap.如果这是用于电话,请假定应用程序的大小为20 MB. 800 * 600 * 4图像,产生1920000字节.较低的图像质量,例如使用RGB_565(与RGB_8888相比,每像素半字节的数量),或预先重新缩放源位图.

Basically, you cannot create a huge Bitmap in memory, for no reason. If this is for phones, assume a 20 MB size for application. An 800*600*4 image, yelds 1920000 bytes. Lower Image quality, such as using RGB_565 (half byte ammount per pixel, compared with RGB_8888), or pre-re-scale your source Bit map.

这篇关于在imageview上显示标签时发生内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆