我使用Universal image loader 1.9.5从网址加载图片 这是适配器
public class BinderDataImg extends BaseAdapter {
static final String KEY_IMG = "img";
LayoutInflater inflater;
List<HashMap<String,String>> imgHashmap;
ViewHolder holder;
ImageLoader imageLoader = ImageLoader.getInstance();
public BinderDataImg(Activity act, List<HashMap<String,String>> map) {
this.imgHashmap = map;
inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return imgHashmap.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.list_img, null);
holder = new ViewHolder();
holder.iv_img =(ImageView)vi.findViewById(R.id.imageViewImg);
vi.setTag(holder);
}
else{
holder = (ViewHolder)vi.getTag();
}
String uri = imgHashmap .get(position).get(KEY_IMG);
imageLoader.displayImage(uri, holder.iv_img);
return vi;
}
static class ViewHolder{
ImageView iv_img;
}
}
这是带有通用图像加载器配置的活动
public class ActImg extends AppCompatActivity {
static final String KEY_IMG = "img";
List<HashMap<String,String>> imgHashmap;
List<ClassImg> imgList = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_img);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.resetViewBeforeLoading(true)
.displayer(new FadeInBitmapDisplayer(300))
.showImageOnLoading(R.drawable.img_loading)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.threadPoolSize(3)
.memoryCache(new WeakMemoryCache())
.diskCacheSize(100 * 1024 * 1024)
.build();
Intent i = getIntent();
CustomListView lvi = (CustomListView)findViewById(R.id.listViewImg);
try {
XmlPullParserImg parser = new XmlPullParserImg();
imgList = parser.parse(getAssets().open(file));
BinderDataImg binderdata = new BinderDataImg(this, imgHashmap);
lvi.setAdapter(binderdata);
}catch(IOException e) {
e.printStackTrace();
}
}
更新:这是logcat
05-03 03:50:38.011 18383-18878 / tttdigital.readmangaonline E / ImageLoader:null
java.lang.OutOfMemoryError
在android.graphics.Bitmap.nativeCreate(原生方法)
在android.graphics.Bitmap.createBitmap(Bitmap.java:689)
在android.graphics.Bitmap.createBitmap(Bitmap.java:666)
在android.graphics.Bitmap.createBitmap(Bitmap.java:599)
at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.considerExactScaleAndOrientatiton(BaseImageDecoder.java:218)
at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:91)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:265)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:238)
在com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:136)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573)
在java.lang.Thread.run(Thread.java:856)
我可以从网址加载图片,但有些图片无法显示,我收到OutOfMemory错误。
如何解决?
答案 0 :(得分:0)
有几种方法可以做到这一点,
1)增加内存缓存大小,例如将其设置为15
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPoolSize(3)
.threadPriority(Thread.MIN_PRIORITY + 3)
.denyCacheImageMultipleSizesInMemory()
.memoryCacheSize(1048576 * 15)
.discCache(new UnlimitedDiscCache(cacheDir))
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.build();
2)减少我在下面使用3的线程池大小
ImageLoaderConfiguration.Builder(context).threadPoolSize(3)
3)同时尝试此设置,因为这适用于滑行,而不是
ARGB_8888
使用RGB_565
.bitmapConfig(Bitmap.Config.RGB_565)
一般情况下,如果您要处理在应用中加载大量图片,则可以在清单文件中使用 largeHeap=true