java 如何在网格视图中将图像和视频显示为缩略图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6507482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:08:43  来源:igfitidea点击:

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

javaandroid

提问by Balban

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

我有一个包含图像和视频路径列表的数据库。我的问题是我必须在GridView. 我已经从数据库中获取了路径列表,但我无法在 GridView 中显示它们。请帮我。这是我的代码。提前致谢

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

}

回答by Balban

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

这对我来说很好用

回答by Alok

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.

你会得到你愿意得到的!希望它会有所帮助。我知道我有点晚了,但可能会对需要它的其他人有所帮助。