使用通用图像加载器和图像刷新时出现内存不足错误 [英] Out Of memory error using Universal Image Loader and images getting refreshed

查看:16
本文介绍了使用通用图像加载器和图像刷新时出现内存不足错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用通用图像加载器在 ListView 中将图像显示为缩略图,但是我遇到内存不足错误,并且当我滚动列表时,当列表未打开时,新视图具有第一个视图的图像滚动阶段,但图像被设置为它应该是的正确图像.

I am using the universal image loader to display the images as thumbnail in ListView however i am getting the out of memory error and when i scroll the list than the new view is having the images of first view when the list was not on scroll stage but than the images gets set to proper image which it should be.

public class NewAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<String> movieThumbnail;
    private ArrayList<String> movieText;
    private static LayoutInflater inflater=null;
    static File cacheDir;

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(activity)
    .memoryCache(new WeakMemoryCache())
    .denyCacheImageMultipleSizesInMemory()
    .discCache(new UnlimitedDiscCache(cacheDir))
    .threadPoolSize(5)
    .imageDownloader(new URLConnectionImageDownloader(120 * 1000, 120 * 1000))
    .enableLogging()
    .build();

    DisplayImageOptions options = new DisplayImageOptions.Builder()
    .cacheOnDisc()
    .cacheInMemory()
    .bitmapConfig(Bitmap.Config.RGB_565)
    .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
    .build();

    private ImageLoader imageLoader= ImageLoader.getInstance();

    public NewAdapter(Activity a, ArrayList<String> movieThumbnail, ArrayList<String> movieText) {
        activity = a;
        /*data=d;*/
        this.movieThumbnail = movieThumbnail;
        this.movieText = movieText;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"JunkFolder");
        else
            cacheDir=activity.getCacheDir();
        if(!cacheDir.exists())
            cacheDir.mkdirs();

        imageLoader.init(config);

    }

    public int getCount() {
        return movieText.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
              vi = inflater.inflate(R.layout.listrow, null);

        TextView text=(TextView)vi.findViewById(R.id.rowListTextView);
        ImageView image=(ImageView)vi.findViewById(R.id.movieImage);
        text.setText(movieText.get(position));
        imageLoader.displayImage(movieThumbnail.get(position), image, options);
        return vi;
    }
}

这里是 ImageView 和 TextView 的 Xml 布局

and here is Xml Layout for ImageView and TextView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text_relativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/latest_list_toggle" />


    <ImageView
        android:id="@+id/movieImage"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="14dp" />

    <TextView
        android:id="@+id/rowListTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/movieImage"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="21dp"
        android:paddingTop="20dp"
        android:text="@string/app_name"
        android:textColor="@android:color/black"
        android:textSize="17sp"
        android:textStyle="bold" />
</RelativeLayout>

推荐答案

在 DisplayImageOptions 中设置 .resetViewBeforeLoading().

Set .resetViewBeforeLoading() in DisplayImageOptions.

这篇关于使用通用图像加载器和图像刷新时出现内存不足错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆