我参考了很多资源,但无法得到正确的答案,
我制作了一个自定义适配器来查看列表视图中的图像。此图像是从存储卡中检索的。一切运行良好,但是当我滚动浏览列表视图时,我得到了 OutOfMemory 异常。我已经发布了用于从 sdcard 检索图像的代码。
public void getFromSdcard() {
File file = new File(
android.os.Environment.getExternalStorageDirectory(),
"Tiles/.NoMedia");
if (file.isDirectory()) {
listFile = file.listFiles();
for (int i = 0; i < listFile.length; i++) {
f.add(listFile[i].getAbsolutePath());
}
}
}
这里 f 是字符串的数组列表,我将它传递给自定义适配器下面是我的自定义适配器的代码。
public class NewImageAdapter extends ArrayAdapter<Image> {
private ArrayList<Image> objects;
String packageName;
Activity act;
public NewImageAdapter(Activity context, int image_layout,
ArrayList<Image> objects) {
super(context, image_layout, objects);
this.act = context;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
Image i = objects.get(position);
if (i != null) {
ImageView iv = (ImageView) v.findViewById(R.id.imagemenu123);
// TextView tv = (TextView) v.findViewById(R.id.commandText);
if (iv != null) {
Bitmap myBitmap = BitmapFactory.decodeFile(i.getImagePath());
iv.setImageBitmap(myBitmap);
// iv.setImageBitmap(i.getImageBitmap());
// tv.setText("Tiles Images");
}
}
return v;
}
}
我的问题的任何解决方案: