我正在尝试使用以下代码从内部sdcard
和drawable
内部显示图像。gridview
但它不起作用。Galaxy Note
由于Memory Error
显示错误的图像而崩溃,emulator
并且还需要大量时间来显示它们。
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory().cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565).build();
适配器类
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;
}
}
更新:我在 gridview 中有大约 30 张图像,一开始它只在整个地方显示第一张图像,并且需要大约 1 分钟才能正确显示所有图像。
日志猫
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)