scrollable list of pictures


Last Updated:

  1. eafkuor

    eafkuor Member This Topic's Starter

    Joined:
    Mar 19, 2010
    Messages:
    7
    Likes Received:
    0
    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 (Text):
    1. public class ImageAdapter implements ListAdapter{
    2.    
    3.     private String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/";
    4.     private File dir=new File(path);
    5.     private File[] files;
    6.     private Context c;
    7.     private static ImageView views[];
    8.     private static Bitmap bitmaps[];
    9.    
    10.     public ImageAdapter(Context context){
    11.         files=dir.listFiles(); //TODO filtrare le foto
    12.         c=context;
    13.        
    14.         views = new ImageView[files.length];
    15.         bitmaps = new Bitmap[files.length];
    16.        
    17.         for(int i=0;i<files.length;i++){
    18.             bitmaps[i]=BitmapFactory.decodeFile(path+files[i].getName());
    19.             views[i]=new ImageView(c);
    20.             views[i].setImageBitmap(Bitmap.createScaledBitmap(bitmaps[i], bitmaps[i].getWidth()/5, bitmaps[i].getHeight()/5, false));
    21.         }
    22.     }
    23.    
    24.  
    25.  
    26.     public View getView (int position, View convertView, ViewGroup parent){
    27.         return views[position];
    28.     }
    29.  
    30.     ...
    31.  
    32.     public Object getItem(int position) {
    33.         return bitmaps[position];
    34.     }
    35.  
    36.     ...
    37. }
    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
     

    Advertisement

Share This Page

Loading...