9

I have been working on creating a Grid View of images, with images being present in the Assets folder. Opening a File from assets folder in android link helped me with using the bitmap to read it. The code am currently having is:

 public View getView(final int position, View convertView, ViewGroup parent) 
{

  try 
    {
     AssetManager am = mContext.getAssets();
     String list[] = am.list("");
     int count_files = imagelist.length;
     for(int i= 0;i<=count_files; i++)
     {
      BufferedInputStream buf = new BufferedInputStream(am.open(list[i]));
      Bitmap bitmap = BitmapFactory.decodeStream(buf);
      imageView.setImageBitmap(bitmap);
      buf.close();
     }
   }   
   catch (IOException e) 
   {
   e.printStackTrace();
   }
  }

My application does read the image from the Assets folder, but it is not iterating through the cells in the grid view. All the cells of the grid view have a same image picked from the set of images. Can anyone tell me how to iterate through the cells and still have different images ?

I have the above code in an ImageAdapter Class which extends the BaseAdapter class, and in my main class I am linking that with my gridview by:

    GridView  gv =(GridView)findViewById(R.id.gridview);
    gv.setAdapter(new ImageAdapter(this, assetlist));       

Thanks a lot for any help in advance, Saran

Community
  • 1
  • 1
Saran
  • 91
  • 1
  • 1
  • 2
  • Is there a particular reason you are putting these in `assets/` rather than as drawable resources? – CommonsWare May 02 '10 at 13:09
  • Actually I want to load these images dynamically, and they are many in number. When I had them in resources, I had memory problem.ContentProvider was getting a little out of hand, so I thought I would try out in assets. - Saran – Saran May 02 '10 at 16:29

2 Answers2

19

Saran, below is what I use to show images in the assets folder with the gallery. I imagine it's the same deal with a gridview:

public class myActivitye extends Activity
{
    private Gallery mGallery;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mGallery = (Gallery) findViewById(R.id.mygalleryinxml);

        //load images into memory
        mBitArray = new Bitmap[4];
        try
        {
            //these images are stored in the root of "assets"
            mBitArray[0] = getBitmapFromAsset("pic1.png");
            mBitArray[1] = getBitmapFromAsset("pic2.png");
            mBitArray[2] = getBitmapFromAsset("pic3.png");
            mBitArray[3] = getBitmapFromAsset("pic4.png");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        mGallery.setAdapter(new GalleryAdapter(this, mBitArray));
    }

    public class GalleryAdapter extends BaseAdapter
    {
        //member variables
        private Context mContext;
        private Bitmap[] mImageArray;

        //constructor
        public GalleryAdapter(Context context, Bitmap[] imgArray)
        {
            mContext = context;
            mImageArray = imgArray;
        }

        public int getCount()
        {
            return mImageArray.length;
        }

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

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

        //returns the individual images to the widget as it requires them
        public View getView(int position, View convertView, ViewGroup parent)
        {
            final ImageView imgView = new ImageView(mContext);

            imgView.setImageBitmap(mImageArray[position]);

            //put black borders around the image
            final RelativeLayout borderImg = new RelativeLayout(mContext);
            borderImg.setPadding(20, 20, 20, 20);
            borderImg.setBackgroundColor(0xff000000);//black
            borderImg.addView(imgView);
            return borderImg;
        }

    }//end of: class GalleryAdapter


    /**
     * Helper Functions
     * @throws IOException 
     */
    private Bitmap getBitmapFromAsset(String strName) throws IOException
    {
        AssetManager assetManager = getAssets();

        InputStream istr = assetManager.open(strName);
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        istr.close();

        return bitmap;
    }
}
Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166
  • by the way, since you want to load images dynamically (I have them hardcoded in my example), if I were you I'd put the images within a folder of "assets" and then use am.list("[your folder name]"); this is because I've seen Android files within assets in addition to my own files – Someone Somewhere May 05 '11 at 19:42
  • 1
    The method getBitmapFromAsset() is neglecting to close the InputStream it opens. – Paul LeBeau Dec 10 '12 at 20:07
3

No need to read all the items every time. Read only the item at the position given in getView method call. And display only that item that time.

BufferedInputStream buf = new BufferedInputStream(am.open(list[position]));
Deepak Swami
  • 3,838
  • 1
  • 31
  • 46
fak1r
  • 31
  • 2