使用 Thumbnails android 在列表视图中显示特定文件夹视频

Posted

技术标签:

【中文标题】使用 Thumbnails android 在列表视图中显示特定文件夹视频【英文标题】:Display specfic folder videos in a listview with Thumbnails android 【发布时间】:2015-09-09 01:46:29 【问题描述】:

我正在尝试在 sdcard 上的文件夹“xyz”中的列表视图中显示视频文件,我已成功显示,但问题是它显示存储在 sdcard 上的所有视频文件,即使在文件夹“abc”之外,实际上我只想存储视频文件在要显示的文件夹“xyz”中。我从 4 天开始就在谷歌上搜索,但没有找到任何解决方案。我的代码是显示所有视频并完美运行:

private Cursor videocursor;  
     private int video_column_index;  
     ListView videolist;  
     int count;  
     String[] thumbColumns = null ;
     File videoFiles;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    init_phone_video_grid();
@SuppressWarnings("deprecation")  
 private void init_phone_video_grid()   
  System.gc();  
  String[] proj =  MediaStore.Video.Media._ID,  
    MediaStore.Video.Media.DATA,  
    MediaStore.Video.Media.DISPLAY_NAME,  
    MediaStore.Video.Media.SIZE ;  
  videocursor = managedQuery( MediaStore.Video.Media.EXTERNAL_CONTENT_URI,proj, 
          null, null,null);  
  count = videocursor.getCount();  
  videolist = (ListView) findViewById(R.id.list);  
  videolist.setAdapter(new VideoAdapter(getApplicationContext()));  
  videolist.setOnItemClickListener(videogridlistener);  
   

 private OnItemClickListener videogridlistener = new OnItemClickListener()   
  public void onItemClick(AdapterView<?> parent, View v, int position,  
    long id)   
   System.gc();  
   video_column_index = videocursor  
     .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);  
   videocursor.moveToPosition(position);  
   String filename = videocursor.getString(video_column_index);  
   Intent intent = new Intent(MainActivity.this, ViewVideo.class);  
   intent.putExtra("videofilename", filename);  
   startActivity(intent);  
    
 ;  


 public class VideoAdapter extends BaseAdapter   
      private Context vContext;  

      public VideoAdapter(Context c)   
       vContext = c;  
        

      public int getCount()   
       return count;  
        

      public Object getItem(int position)   
       return position;  
        

      public long getItemId(int position)   
       return position;  
        

      public View getView(int position, View convertView, ViewGroup parent)   
       System.gc();  
       ViewHolder holder;  
       String id = null;  
       convertView = null;  
       if (convertView == null)   
        convertView = LayoutInflater.from(vContext).inflate(R.layout.listitem, parent, false);  
        holder = new ViewHolder();  
        holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);  
        holder.txtSize = (TextView) convertView.findViewById(R.id.txtSize);  
        holder.thumbImage = (ImageView) convertView.findViewById(R.id.imgIcon);  

        video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);  
        videocursor.moveToPosition(position);  
        id = videocursor.getString(video_column_index);  
        video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);  
        videocursor.moveToPosition(position);  
        // id += " Size(KB):" +  // videocursor.getString(video_column_index);  
        holder.txtTitle.setText(id);  
        holder.txtSize.setText(" Size(KB):" + videocursor.getString(video_column_index));  

        String[] proj =  MediaStore.Video.Media._ID,  
          MediaStore.Video.Media.DISPLAY_NAME,  
          MediaStore.Video.Media.DATA ;  
        @SuppressWarnings("deprecation")  

        Cursor cursor = managedQuery(  
          MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj,  
          MediaStore.Video.Media.DISPLAY_NAME + "=?",  
          new String[]  id , null);  
        cursor.moveToFirst();  

        long ids = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media._ID));  

        ContentResolver crThumb = getContentResolver();  
        BitmapFactory.Options options = new BitmapFactory.Options();  
        options.inSampleSize = 1;  
        Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(  
          crThumb, ids, MediaStore.Video.Thumbnails.MICRO_KIND,  
          options);  
        holder.thumbImage.setImageBitmap(curThumb);  
        curThumb = null;  

       
       return convertView;  
        
       

     static class ViewHolder   

      TextView txtTitle;  
      TextView txtSize;  
      ImageView thumbImage;  
               

【问题讨论】:

【参考方案1】:

试试这个

public static final String[] VIDEO_PROJECTION = MediaStore.Video.Media._ID, MediaStore.Video.Media.TITLE,
        MediaStore.Video.Media.DATA;

public static final Uri VIDEO_SOURCE_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

使用内容解析器查询数据

    CursorLoader cursorLoader = new CursorLoader(getActivity(), VIDEO_SOURCE_URI, VIDEO_PROJECTION, MediaStore.Video.Media.DATA + " like ? ", new String[]"%FOLDER_NAME%",
            MediaStore.Video.Media.DATA + " COLLATE NOCASE ASC;");

它会从 FOLDER_NAME 中检索所有视频文件。由于可以有一个或多个具有名称的文件夹,因此最好提供绝对路径。

【讨论】:

No Bro ..... :( 这段代码也不行......你能不能给我一些比这更棒的东西? 根据这个解决方案....不显示任何错误/异常也不显示结果,简单显示白屏:( 然后尝试将 FOLDER_NAME 传递为空并检查它 我什么时候给空.....它现在会显示任何输出......?我需要带有缩略图的录制视频列表 然后尝试给出特定的文件夹名称,如蓝牙

以上是关于使用 Thumbnails android 在列表视图中显示特定文件夹视频的主要内容,如果未能解决你的问题,请参考以下文章

Thumbnails为图片加水印

关于使用带有远程存储的 sorl-thumbnails 的 celery 的指针?

使用Thumbnails工具对图片进行缩放,压缩

删除使用easy-thumbnails Django App生成的所有缩略图

easy-thumbnails:在缩略图别名中使用 scale_and_crop 图像处理器

wordpress插件multiple-post-thumbnails实现多缩略图教程