我有一个列表视图,显示目录中的图像,它正在加载图像,但滚动后向上滚动,我必须从屏幕上移开我的手指,然后将其放回屏幕滚动备份。如果滚动太快,应用程序崩溃,logcat说OutOfMemoryError。
这是我的代码:
package com.search.visual;
import java.io.File;
import java.io.FilenameFilter;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
public class history extends ListActivity{
private String dir = ".vis";
private File images;
private File [] imageList;
private File item;
private Uri [] ImageRef;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
loadPics();
}
public class ImageAdapter extends BaseAdapter {
int Background;
private Context con;
public ImageAdapter(Context c) {
con = c;
TypedArray attr = con.obtainStyledAttributes(R.styleable.HelloGallery);
Background = attr.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
attr.recycle();
}
public int getCount() {
return ImageRef.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
imageView = new ImageView(con);
imageView.setImageURI(ImageRef[position]);
imageView.setLayoutParams(new ListView.LayoutParams(75, 75));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(Background);
return imageView;
}
}
protected void onListItemClick(ListView l, View v, int position, long id) {
item = imageList[position];
Intent i = new Intent("com.search.visual.IMAGE");
i.putExtra(Intent.EXTRA_STREAM, item);
startActivity(i); //StIntring imname = (String) ;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
loadPics();
super.onResume();
}
public void loadPics(){
images = Environment.getExternalStoragePublicDirectory(dir);
imageList = images.listFiles(new FilenameFilter() {
public boolean accept(File dir, String filename) {
// TODO Auto-generated method stub
return (filename.endsWith(".jpg"));
}
});
if(imageList.length > 0 ){
ImageRef = new Uri[imageList.length];
for(int i = 0; i<imageList.length; i++){
ImageRef[i] = Uri.parse(imageList[i].getAbsolutePath());
}
setListAdapter(new ImageAdapter(this));
}else{
Intent i = new Intent("com.search.visual.ERROR");
startActivity(i);
}
}
}
如果有人知道为什么会发生这种情况,那将非常感谢你的帮助
答案 0 :(得分:1)
滚动列表视图时,所有图像都存储在native heap
和recycler of native heap is too lazy
中。因此,从listview中滚动图像收集垃圾的速度很慢,这就是为什么你的device's heap is become full
会得到OutOfMemory
异常。
所以我认为你sholud try your own recycler
用于处理应用程序中的图像和使用过的图像的free the memory
。
你必须回收位图。位图实现是本机的,因此java对象很小并且是垃圾收集的不良候选者,但仍然分配了内存。塔卡期待Bitmap.recycle()
或者只是尝试Lazzy Loading images in Listview
。
Android: Strange out of memory issue和OutOfMemory exception appears while scrolling the list of images
感谢。