【发布时间】:2014-04-17 00:22:01
【问题描述】:
作为一个完整的 Android 新手,并且(诚然)不是最强大的程序员 - 我想就将缩略图图像加载到一个位图数组中寻求一些建议,该数组被加载到一个自定义适配器中。
缩略图非常小(大约 5KB)。
我将缩略图添加到异步任务中的位图数组。我正在使用可绘制的虚拟图像。所以我用虚拟图像加载整个列表(稍后我会加载实际图像)。
如果用户浏览包含 200 多张图片的文件夹,我会担心。我可能会遇到内存不足错误。我想要一种方法来防止这种情况,也许只加载可见显示中需要的内容,并在需要时加载更多内容?
我已经阅读了很多关于回收位图的其他问题和建议,但我仍然不确定从哪里开始。
@Override
protected Boolean doInBackground(DbxFileSystem... params) {
//Opens thumbnails for each image contained in the folder
try {
DbxFileSystem fileSystem = params[0];
Bitmap image=null;
int loopCount=0; //I use this to identify where in the adapter the real image should go
for (DbxFileInfo fileInfo: fileSystem.listFolder(currentPath)) {
try{
if(!fileInfo.isFolder)
{
image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
pix.add(image);
paths.add(fileInfo.path);
loopCount++;
}
else
{
//must be a folder if it has no thumb, so add folder icon
image = BitmapFactory.decodeResource(getResources(), R.drawable.dbfolder);
pix.add(image);
paths.add(fileInfo.path);
loopCount++;
}
}
catch(Exception e)
{
e.printStackTrace();
}
System.gc();
}
}
catch (Exception e) {
e.printStackTrace();
return false;
} finally {
loadingDialog.dismiss();
}
return true;
}
这是来自自定义适配器的 getView:
public View getView(final int position, View arg1, ViewGroup arg2) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = arg1;
ViewHolder holder;
if (arg1 == null) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.title = (TextView) v.findViewById(R.id.filename);
holder.iconImage = (ImageView) v.findViewById(R.id.list_image);
holder.checkbox = (CheckBox)v.findViewById(R.id.checkBox1);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.title.setText(folderName.get(position).toString());
holder.iconImage.setImageBitmap(images.get(position));
【问题讨论】: