How to list all videos from specific folder in android

Giridharan picture Giridharan · Oct 4, 2013 · Viewed 15k times · Source

I am working on video recording application.I want to list the videos which I would be stored in particular folder.By the following code,I can able to fetch all videos from mobile.But i need to list the videos from particular folder.Can anyone guide me please.Thanks in Advance

public class VideoListActivity extends Activity {
    private Cursor videocursor;
    private int video_column_index;
    ListView videolist;
    int count;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);
        init_phone_video_grid();
    }

    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.listView1);
        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(VideoListActivity.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();
            TextView tv = new TextView(vContext.getApplicationContext());
            String id = null;
            if (convertView == null) {
                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);


                        ImageView iv = new ImageView(vContext);
                        ContentResolver crThumb = getContentResolver();
                        BitmapFactory.Options options=new BitmapFactory.Options();
                        options.inSampleSize = 1;
                        Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, position, MediaStore.Video.Thumbnails.MICRO_KIND, options);
                        iv.setImageBitmap(curThumb);

                tv.setText(id);

            } else
                tv = (TextView) convertView;
            return tv;
        }
    }
}

Answer

Mike Cruze picture Mike Cruze · Jan 11, 2019

Try the following code:

public static ArrayList<File> getListFiles(File parentDir) {
    ArrayList<File> inFiles = new ArrayList<File>();
    File[] files;
    files = parentDir.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.getName().endsWith(".mp4") ||
                    file.getName().endsWith(".gif")) {
                if (!inFiles.contains(file)) inFiles.add(file);

                if (!inFiles.contains(file)) inFiles.add(file);
            }
        }
    }
    return inFiles;
}

Use :

private static final String WHATSAPP_STATUSES_LOCATION =  
"/storage/emulated/0/yourfoldername";


getListFiles(new File(WHATSAPP_STATUSES_LOCATION));