必須在recycleview上顯示圖像和貼紙列表(webp格式)。在imageview上顯示貼紙時發生內存不足
要在imageView上顯示貼紙,請使用[repository](https://github.com/EverythingMe/webp-android)。該存儲庫是在這個交(WebP for Android)
貼紙文件是從外部存儲readed建議 溶液之一,則轉換爲字節陣列中,通過使用存儲庫的庫,字節數組轉換爲位圖,最後的位圖被示出在ImageView的。下面的代碼轉換標籤文件爲位圖
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」方法被調用,這個方法做工精細的大部分時間,但有時「內存不足」的錯誤是在發生這種情況方法。大多數時候,這個錯誤是發生在這些線路
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);
我發現貼紙的文件,它的字節數組長度大,我可以減少標籤的文件大小以編程方式?我想找到解決方案,以減少字節數組的大小。
使用RGB_565會導致圖像質量下降,特別是如果在貼紙中使用文本 –
就像回答一樣,您遇到的主要問題是創建比您的顯示更大的「位圖」。要麼不加載它,改變原始的位圖或文件,然後加載縮放版本,或者獲得你的'View'大小,然後根據這個大小加載'Bitmap'。沒有使用「預安卓4.0」默認庫。原因是這樣的,如果你需要在低資源設備上顯示一個大的'Bitmap',請在google上尋找幫助,但是不要指望'Magically.load(largeBitmap)'函數 – Bonatti