2

I have a database which contains list of image and video path. My problem is I have to show all the images and video in a GridView. I have taken list of path from database but I am not able to show them in GridView. Please help me. Here is my code. Thanks in Advance

 public class GridGallery extends Activity
{

    ArrayList<String>list;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_gallery);
        DataModel dbModel = new DataModel(this);
        list = dbModel.selectAll();

        GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
        sdcardImages.setAdapter(new ImageAdapter(this));

    }


     /**
     * Adapter for our image files.
     */
    private class ImageAdapter extends BaseAdapter {

        private final Context context; 

        public ImageAdapter(Context localContext) {
            context = localContext;
        }

        public int getCount() 
        {
            return list.size();
        }
        public Object getItem(int position) 
        {
            return position;
        }
        public long getItemId(int position) 
        {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) 
        {
            ImageView picturesView;
            if (convertView == null) {
                picturesView = new ImageView(context);

                for(int i=0;i<list.size();i++)
                {
                    Bitmap mBitmap = BitmapFactory.decodeFile(list.get(i));
                    picturesView.setImageBitmap(mBitmap);
                }
                picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                picturesView.setPadding(8, 8, 8, 8);
                picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
            }
            else 
            {
                picturesView = (ImageView)convertView;
            }
            return picturesView;
        }
    }

}
Balban
  • 718
  • 3
  • 9
  • 24
  • 2
    You need to be more specific. Is there a reason why the images and videos are not showing up. What **exactly** are you having difficulty with here? Are you able to output anything to a log? Please provide some code to help us help you – O'Mutt Jun 28 '11 at 14:06
  • I am only getting the Three thumbnails of single file – Balban Jun 28 '11 at 14:22
  • print out your list in the log and see what it says `for (int i = 0; i < list.size(); i++){ Log.e("LOGTAG LIST", list.get(i)); }` – O'Mutt Jun 28 '11 at 16:40
  • @MutMatt:- I have removed the for loop and used:- Bitmap mBitmap = BitmapFactory.decodeFile(list.get(position)); So it is working fine for images but it is not showing video's thumbnails – Balban Jun 29 '11 at 04:52

2 Answers2

5

Yes..

I got the answer after a long experiment: here it is:

public class GridGallery extends Activity
{
    ArrayList<String>list;    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_gallery);
        DataModel dbModel = new DataModel(this);
        list = dbModel.selectAll();

        GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
        sdcardImages.setAdapter(new ImageAdapter(this));    
    }    

     /**
     * Adapter for our image files.
     */
    private class ImageAdapter extends BaseAdapter {

        private final Context context;     
        public ImageAdapter(Context localContext) {
            context = localContext;
        }

        public int getCount() 
        {
            return list.size();
        }
        public Object getItem(int position) 
        {
            return position;
        }
        public long getItemId(int position) 
        {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) 
        {
            ImageView picturesView;
            if (convertView == null) {
                picturesView = new ImageView(context);    
                 if(list.get(position).contains(".jpg"))
                {
                     bitmap = BitmapFactory.decodeFile(list.get(position)); //Creation of Thumbnail of image
                }
                else if(list.get(position).contains(".mp4"))
                {
                    bitmap = ThumbnailUtils.createVideoThumbnail(list.get(position), 0); //Creation of Thumbnail of video
                }
                picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                picturesView.setPadding(8, 8, 8, 8);
                picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
            }
            else 
            {
                picturesView = (ImageView)convertView;
            }
            return picturesView;
        }
    }

}

This works fine for me

Maveňツ
  • 1
  • 12
  • 50
  • 89
Balban
  • 718
  • 3
  • 9
  • 24
  • How are you using ThumbnailUtils? I no longer see that import available... was android.media.ThumbnailUtils. Anyone know if this is still useable? – JLB Jul 11 '11 at 19:17
  • yes.. this is available and working fine for me... I sort out my issue by using this – Balban Jul 12 '11 at 05:30
  • I've realized this became available in api 8. I wasn't seeing this because I was using 7. – JLB Jul 13 '11 at 14:19
  • 1
    @balbanshah : do u have any idea how to capture frames from video? – CoDe Aug 09 '12 at 10:40
  • @balbanshah is there any way to create the thumbnail of other documents like pdf,word,.... – KJEjava48 Feb 26 '16 at 12:09
1

You have to just add this line before your return of the picturesView

Just add this

picturesView.setImageBitmap(bitmap);

just after this :

picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));

You will get it what you are willing to get! Hope it'll help. I know I'm bit late but may be it'll be helpful someone else who is need of it.

Alok
  • 8,452
  • 13
  • 55
  • 93