我正在開發與Facebook和Gatlk連接的聊天應用程序,使用ASMACK API獲取聯繫人和VCard並在ListView中顯示它們。Android OutOfMemory將圖像字節數組轉換爲可繪製
從位於BYTE ARRAY的VCard獲取聯繫人圖像。我需要將此字節數組轉換爲圖像(位圖或可繪製),但在從圖像字節數組創建Drawable時發生OutOfMemory異常,並且在滾動ListView時發生。
下面是代碼片段,也嘗試將其轉換爲位圖,但與Bitmap outofmemory異常來更頻繁。嘗試使用bitmap.recyle()方法作爲解決方案在論壇上提供此類問題。但使用回收()大部分地方得到例外,如「使用回收圖像」。
這裏如果日誌:
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
請讓我知道的最好的辦法,以字節組轉換成位圖或繪製對象。 代碼段是在這裏:
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陣列工作的原因? – ab11 2012-08-09 14:38:48
成功登錄後,您應該將用戶的頭像設置爲'SDCard'或者存儲在數據庫中,例如'Contacts app',並使用'AsyncTask'在列表視圖中加載。我做到了,我的應用程序非常好 – 2012-08-09 15:01:39
頭像字節數組我在DB中存儲。現在我已經在字節數組中應用了壓縮,但是圖像似乎被扭曲了,有時候還會出現OutOfMemory。 – Aparna 2012-08-10 15:01:03