OutOfMemoryError通用图像加载器

时间:2015-11-30 11:26:12

标签: android out-of-memory universal-image-loader

尝试加载超过20张图片时,我收到OutOfMemoryError

private Bitmap getImage(String url)
{


    ImageLoader imageLoader=ImageLoader.getInstance();
    bitmap =imageLoader.loadImageSync(url);

    return bitmap;
}

    11-30 13:12:59.609 11751-12088/com.example.navbartest E/ImageLoader: Failed to allocate a 1464012 byte allocation with 286292 free bytes and 279KB until OOM
java.lang.OutOfMemoryError: Failed to allocate a 1464012 byte allocation with 286292 free bytes and 279KB until OOM 
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:634)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:610)
at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:83) 
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:265)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:223)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:136)
at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:297)
at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:209)
 at com.nostra13.universalimageloader.core.ImageLoader.loadImage(ImageLoader.java:528)
 at com.nostra13.universalimageloader.core.ImageLoader.loadImage(ImageLoader.java:489) 
  at com.nostra13.universalimageloader.core.ImageLoader.loadImageSync(ImageLoader.java:602)
 at com.nostra13.universalimageloader.core.ImageLoader.loadImageSync(ImageLoader.java:543)
 at com.example.navbartest.Parsers.InitNews.getImage(InitNews.java:58)
 at com.example.navbartest.Parsers.InitNews.<init>(InitNews.java:33)
 at com.example.navbartest.Parsers.InitNews.onLoadMore(InitNews.java:101)
 at com.example.navbartest.Fragments.MoneyFragment$onLoadMore.doInBackground(MoneyFragment.java:76)
 at com.example.navbartest.Fragments.MoneyFragment$onLoadMore.doInBackground(MoneyFragment.java:72)at android.os.AsyncTask$2.call(AsyncTask.java:292)
 at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)at java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:0)

做这样的事情

private Bitmap getImage(String url)
{
    DisplayImageOptions mOptions=new DisplayImageOptions.Builder().cacheOnDisk(true)
            .showImageOnFail(R.drawable.no_image)
            .showImageOnLoading(R.drawable.no_image)
            .showImageForEmptyUri(R.drawable.no_image).cacheInMemory(true)
            .bitmapConfig(Bitmap.Config.RGB_565).build();

    ImageLoader imageLoader=ImageLoader.getInstance();
    bitmap =imageLoader.loadImageSync(url,mOptions);

    return bitmap;
}