Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OutOfMemory将图像字节数组转换为可绘制的_Android_Bitmap_Bytearray_Out Of Memory - Fatal编程技术网

Android OutOfMemory将图像字节数组转换为可绘制的

Android OutOfMemory将图像字节数组转换为可绘制的,android,bitmap,bytearray,out-of-memory,Android,Bitmap,Bytearray,Out Of Memory,我正在开发与Facebook和Gatlk连接的聊天应用程序,使用ASMACK API获取联系人和VCard,并在ListView中显示它们 从字节数组中的VCard获取联系人图像。我需要将此字节数组转换为图像(位图或可绘制),但在从图像字节数组创建可绘制时以及在滚动ListView时会出现内存异常 下面是代码片段,也尝试将其转换为位图,但使用Bitmap outofmemory时出现异常的频率更高。尝试使用bitmap.recyle()方法作为论坛上针对此类问题提供的解决方案。但是使用recyc

我正在开发与Facebook和Gatlk连接的聊天应用程序,使用ASMACK API获取联系人和VCard,并在ListView中显示它们

从字节数组中的VCard获取联系人图像。我需要将此字节数组转换为图像(位图或可绘制),但在从图像字节数组创建可绘制时以及在滚动ListView时会出现内存异常

下面是代码片段,也尝试将其转换为位图,但使用Bitmap outofmemory时出现异常的频率更高。尝试使用bitmap.recyle()方法作为论坛上针对此类问题提供的解决方案。但是使用recycle()大多数地方都会出现类似“使用回收的图像”这样的异常情况

如果日志:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget
 E/dalvikvm-heap(26048): 10000-byte external allocation too large for this process.
 E/GraphicsJNI(26048): VM won't let us allocate 10000 bytes
请让我知道在位图或可绘制中转换bytearray的最佳方法。 代码片段如下所示:

public static Drawable createDrawableImageFromByteArray(Context context, byte[] imageByteArray){
    Drawable drawable = null;
    try{
        if(imageByteArray != null){                                                                                                 
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;                                                            
            int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)55);
            int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)55);

            if (heightRatio > 1 || widthRatio > 1){
                if (heightRatio > widthRatio){
                    bmpFactoryOptions.inSampleSize = heightRatio;  
                } else {
                    bmpFactoryOptions.inSampleSize = widthRatio;   
                }   
            }                                              
            bmpFactoryOptions.inJustDecodeBounds = false;
            bmpFactoryOptions.inPurgeable = true;
            bmpFactoryOptions.inInputShareable = true;
            drawable = Drawable.createFromResourceStream(context.getResources(), new TypedValue(), new ByteArrayInputStream(imageByteArray), "testimg", bmpFactoryOptions);
        }
    }catch(OutOfMemoryError e){
        Utils.debugLog("****** createBitmaException :: " + e);
    }catch(Exception e){
        Utils.debugLog("****** createBitmaException :: " + e);
    }
    return drawable;                             
}

谢谢

请参阅此处的文档。它应该对你有帮助


10k字节不是很大。可能您的应用程序在其他地方使用了大量内存,这就是为什么它没有足够的内存来处理10k阵列的原因?您应该在成功登录后立即将用户的化身设置为
SD卡
,或者像
联系人应用程序
一样存储在DB中,并使用
异步任务
在listview中加载。我这样做了,我的应用程序是相当好的阿凡达字节数组,我存储在数据库中。现在我已经在字节数组中应用了压缩,但图像似乎被扭曲了,仍然有时会从内存中消失。我已经通过上面的链接尝试了,但仍然得到了相同的结果。