Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

java - How to show image and video as thumbnail in grid view?

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...