Display specfic folder videos in a listview with Thumbnails android

1.6k views Asked by At

I am trying to display video files in listview from folder 'xyz' on sdcard, i have successfully displayed but problem is that it displays all video files stored on sdcard even out of folder 'abc' actually i want only video files stored in folder 'xyz' to be displayed. I am googling since 4 days but didn't find any solution for that. My code is that is showing all videos and working perfectly :

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

There are 1 answers

8
Kartheek On BEST ANSWER

Try this

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;

Querying the data using the content resolver

    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;");

It retrievers all the video files from the FOLDER_NAME. Since there can be one or more folders with a name so it is better to provide absolute path.