• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps scrollable list of pictures

eafkuor

Lurker
Imagine I have some pictures in the folder /sdcard/Pictures/ and I want a scrollable list of all those pictures.
I implemented ListAdapter like this:

Code:
public class ImageAdapter implements ListAdapter{
    
    private String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/";
    private File dir=new File(path);
    private File[] files;
    private Context c;
    private static ImageView views[];
    private static Bitmap bitmaps[];
    
    public ImageAdapter(Context context){
        files=dir.listFiles(); //TODO filtrare le foto
        c=context;
        
        views = new ImageView[files.length];
        bitmaps = new Bitmap[files.length];
        
        for(int i=0;i<files.length;i++){
            bitmaps[i]=BitmapFactory.decodeFile(path+files[i].getName());
            views[i]=new ImageView(c);
            views[i].setImageBitmap(Bitmap.createScaledBitmap(bitmaps[i], bitmaps[i].getWidth()/5, bitmaps[i].getHeight()/5, false));
        }
    }
    


    public View getView (int position, View convertView, ViewGroup parent){
        return views[position];
    }

    ...

    public Object getItem(int position) {
        return bitmaps[position];
    }

    ...
}

And in the activity i linked a ListView to an instance of this class (ImageAdapter).
It kind of works, but when there are more than 6 or 7 pictures, it runs out of memory (BitmapFactory) and throws an exception.

Is there a better way to do this?
Thanks
 
Back
Top Bottom