问题
I am trying to display images from sdcard
and drawable
inside gridview
using the following code.But it doesn't works.It crashes in Galaxy Note
due to Memory Error
but displaying wrong images in emulator
and also takes alot of time to display them.
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory().cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565).build();
Adapter Class
public class GridAdapter extends BaseAdapter {
public GridAdapter(Context ctx) {
// TODO Auto-generated constructor stub
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(ctx));
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mImages.size();
}
@Override
public Object getItem(int pos) {
// TODO Auto-generated method stub
return mImages.get(pos);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public class ViewHolder {
ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = getLayoutInflater().inflate(R.layout.grid_image,
parent, false);
holder.image = (ImageView) convertView
.findViewById(R.id.gridImage);
holder.image.setLayoutParams(new LinearLayout.LayoutParams(
width / 2, width / 2));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (mImages.get(position).getImageSource().equals("sdcard")) {
imageLoader.displayImage(mImages.get(position).getImages(),
holder.image,options);
} else {
String url = "drawable://"
+ Integer.parseInt(mImages.get(position).getImages());
imageLoader.displayImage(url, holder.image,options);
}
return convertView;
}
}
Update : I have around 30 images in gridview, at the beginning it just displays first image all over the place and it takes around 1 minute to display all the images correctly.
Logcat
04-16 21:41:16.522: E/dalvikvm-heap(738): Out of memory on a 1042024-byte allocation.
04-16 21:41:16.565: I/dalvikvm(738): "pool-1-thread-1" prio=4 tid=15 RUNNABLE
04-16 21:41:16.565: I/dalvikvm(738): | group="main" sCount=0 dsCount=0 obj=0x4054f720 self=0x279080
04-16 21:41:16.572: I/dalvikvm(738): | sysTid=762 nice=10 sched=0/0 cgrp=bg_non_interactive handle=2593208
04-16 21:41:16.591: I/dalvikvm(738): | schedstat=( 47274229556 127781868212 1951 )
04-16 21:41:16.591: I/dalvikvm(738): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:~91)
04-16 21:41:16.591: I/dalvikvm(738): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:216)
04-16 21:41:16.591: I/dalvikvm(738): at android.graphics.Bitmap.nativeCompress(Native Method)
04-16 21:41:16.633: I/dalvikvm(738): at android.graphics.Bitmap.compress(Bitmap.java:589)
04-16 21:41:16.642: I/dalvikvm(738): at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromDrawable(BaseImageDownloader.java:188)
04-16 21:41:16.642: I/dalvikvm(738): at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:92)
04-16 21:41:16.642: I/dalvikvm(738): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.downloadImage(LoadAndDisplayImageTask.java:340)
04-16 21:41:16.652: I/dalvikvm(738): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryCacheImageOnDisc(LoadAndDisplayImageTask.java:307)
04-16 21:41:16.652: I/dalvikvm(738): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:249)
04-16 21:41:16.652: I/dalvikvm(738): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:131)
04-16 21:41:16.652: I/dalvikvm(738): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-16 21:41:16.713: I/dalvikvm(738): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-16 21:41:16.713: I/dalvikvm(738): at java.lang.Thread.run(Thread.java:1019)
来源:https://stackoverflow.com/questions/16041676/unable-to-display-images-into-gridview-using-universal-image-loader