我使用從這link lazyloading從url設置圖像,我有非常大的位圖..在我的列表視圖有很多圖像加載和當我滾動列表視圖20-30這是錯誤的outofmemory ..如何,我可以解決這個錯誤..?從url加載大圖像時OutOfMemory錯誤
回答
發生此錯誤,因爲圖像很大。你可以通過下面提到的方法有效地處理它。
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
檢查該庫的延遲加載Android-Universal-Image-Loader。 該庫爲異步圖像加載,緩存和顯示提供可重複使用的工具。它最初基於lazyloading項目,並且自那時以來大大地進行了重構和改進。
您需要確保在可能的情況下進行縮減採樣,並且由於您使用的是ListView,因此您還可能通過將圖像緩存到內存或磁盤中來獲益。
這裏閱讀最佳做法:Displaying Bitmaps Effeciently
如果您正在尋找實施的最佳實踐考慮使用助手庫加載您的圖像更簡單的方法。這裏是一個很好的一個:Android-Universal-ImageLoader
Outof的MemoryError
時內存的請求時使用可用的平臺資源不能滿足拋出。這樣的請求可以由正在運行的應用程序或VM的內部功能來完成。
因此,您的映像太大,超過了VM分配的內存。縮小圖像或使用下面的庫。
您可以使用
懶列表
通用圖像裝載機
懶列表是從SD卡或者使用FOMR服務器URL的圖像延遲加載。這就像按需加載圖像。
圖像可以緩存到本地SD卡或手機mmeory。網址被認爲是關鍵。如果密鑰存在於sdcard中,則顯示來自SD卡的圖像,否則通過從服務器下載顯示圖像並將其緩存到您選擇的位置。緩存限制可以設置。您也可以選擇自己的位置來緩存圖像。緩存也可以被清除。
而不是用戶等待下載大圖像,然後顯示懶惰列表按需加載圖像。由於緩存的圖像區域可以離線顯示圖像。
https://github.com/thest1/LazyList。懶惰列表
在你getview
imageLoader.DisplayImage(imageurl, imageview);
ImageLoader的顯示方法
public void DisplayImage(String url, ImageView imageView) //url and imageview as parameters
{
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url); //get image from cache using url as key
if(bitmap!=null) //if image exists
imageView.setImageBitmap(bitmap); //dispaly iamge
else //downlaod image and dispaly. add to cache.
{
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
} 到懶惰列表的替代方案是通用圖像裝載機
https://github.com/nostra13/Android-Universal-Image-Loader。它基於Lazy List(基於相同原理)。但它有很多其他配置。我寧願使用通用圖像加載程序因爲它給你更多的配置選項。如果downlaod失敗,您可以顯示錯誤圖像。可以顯示圓角的圖像。可以緩存在光盤或內存上。可以壓縮圖像。
在您的自定義適配器構造
File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");
// Get singletone instance of ImageLoader
imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
// You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.enableLogging()
.build();
// Initialize ImageLoader with created configuration. Do it once.
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_id)//display stub image
.cacheInMemory()
.cacheOnDisc()
.displayer(new RoundedBitmapDisplayer(20))
.build();
在你getView()
ImageView image=(ImageView)vi.findViewById(R.id.imageview);
imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options
你可以用其他選項配置,以滿足您的需求。
這是作者的話。(直接從下主題有用信息的鏈接)
如果經常使用的通用圖像裝載機旁邊(全部或幾個,然後嘗試獲得的OutOfMemoryError在你的應用程序 ):
減少配置(.threadPoolSize(...))中的線程池大小。建議使用1 - 5。 在顯示選項中使用.bitmapConfig(Bitmap.Config.RGB_565)。 RGB_565中的位圖比ARGB_8888消耗的內存少2倍。
在配置中使用.memoryCache(新的WeakMemoryCache())或者在顯示選項中全部禁用內存緩存(不要調用.cacheInMemory())。
在顯示選項中使用.imageScaleType(ImageScaleType.IN_SAMPLE_INT)。或者嘗試.imageScaleType(ImageScaleType.EXACTLY)。
避免使用RoundedBitmapDisplayer。它會創建一個帶有ARGB_8888配置的新Bitmap對象,用於在工作中顯示。
隨着延遲加載/通用圖像裝載機可以使用爲平滑滾動和性能視圖持有人。
http://developer.android.com/training/improving-layouts/smooth-scrolling.html。
- 1. 從URL加載圖像到位圖時的OutOfMemory
- 2. 如何防止Outofmemory錯誤加載大圖像?
- 3. 從URL加載大量圖像
- 4. 從URL加載.svg圖像
- 5. Android從URL加載圖像
- 6. ImageSwitcher從url加載圖像
- 7. 從blob加載圖像時顯示默認和錯誤圖像
- 8. 編寫大文件時OutofMemory錯誤Android
- 9. IE7圖像從類錯誤加載
- 10. 當從url解碼圖像時出現OutOfMemory
- 11. 當在列表視圖中使用圖像時Xamarin.forms OutOfMemory錯誤
- 12. 從Android中加載圖像從URL
- 13. 在簡單圖像中從url加載圖像時出錯php類
- 14. 在.Net中加載圖像時發生OutOfMemory異常
- 15. OutOfMemory異常 - 用於使用picasso加載圖像時的adapter.notifiyDataSetChanged()
- 16. 加載圖像時出錯
- 17. 錯誤的圖像加載時在列表視圖中加載圖像
- 18. 如何在加載大圖像時顯示加載圖像?
- 19. 加載從文件流可提拉引起OutOfMemory錯誤
- 20. GATE PersistenceManager.loadObjectFromFile加載.gapp文件時outofmemory錯誤
- 21. 在JUnit中加載彈簧bean時發生OutOfMemory錯誤
- 22. OutofMemory LinkedList添加錯誤
- 23. CustomListView與位圖 - OutOfMemory錯誤
- 24. Dlib圖像加載錯誤,無法加載圖像
- 25. 圖像未從URL加載到Mvx.MvxImageView
- 26. 如何從URL加載圖像在Xamarin.iOS
- 27. AS3 - 從URL加載圖像座標
- 28. C#Xamarin從url加載圖像
- 29. 加載圖像從URL中的Android
- 30. iPhone NSMutableArray從url加載背景圖像
你有什麼嘗試? – 2013-04-04 13:13:57