0

i have a listview that displays images from a directory and it is loading the images fine but then when scrolling in order to scroll up after scrolling down i have to remove my finger from the screen and place it back on the screen to scroll back up. also if scroll is too fast application crashes with logcat saying OutOfMemoryError.

here is my code:

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);
    }
}


}

if anyone one has any clue as to why this is happening your help would be greatly appreciated

Roy James Schumacher
  • 636
  • 2
  • 11
  • 27

1 Answers1

1

When you scroll the listview all the images are stored in native heap and the recycler of native heap is too lazy. So it is slow in collect garbage from the scrolled images from listview and that's why your device's heap is become full and you get the OutOfMemory Exception.

So I think you sholud try your own recycler for garbage the images in application and free the memory of used images.

you have to recycle bitmap. The bitmap implementation is native, so the java object is small and a poor candidate for java garbage collection but the memory is still allocated. Taka a look to Bitmap.recycle()

or else just try to Lazzy Loading images in Listview.

Android: Strange out of memory issue and OutOfMemory exception appears while scrolling the list of images

Thanks.

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
  • I sorted the problem by swapping the `Uri Array` to a `Bitmap array` this has made the memory problem seem to disappear and also the scrolling problem is gone to thank you for your help and advice. – Roy James Schumacher Oct 18 '11 at 20:22