Skip to content

Instantly share code, notes, and snippets.

@vxhviet
Last active September 13, 2020 19:26
Show Gist options
  • Save vxhviet/bfcf54d060ee2b123c86fae2143dbbcf to your computer and use it in GitHub Desktop.
Save vxhviet/bfcf54d060ee2b123c86fae2143dbbcf to your computer and use it in GitHub Desktop.
Get all video's path on the device

Source: StackOverflow, StackOverflow

Question: How to get the path of all videos stored on device.

Answer:

private String[] getAllVideoPath(Context context) {
        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String[] projection = { MediaStore.Video.VideoColumns.DATA };
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
        ArrayList<String> pathArrList = new ArrayList<String>();
        //int vidsCount = 0;
        if (cursor != null) {
            //vidsCount = cursor.getCount();
            //Log.d(TAG, "Total count of videos: " + vidsCount);
            while (cursor.moveToNext()) {
                pathArrList.add(cursor.getString(0));
                //Log.d(TAG, cursor.getString(0));
            }
            cursor.close();
        }
        
        return pathArrList.toArray(new String[pathArrList.size()]);
    }

Sorted newest Video first:

        ...
        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String[] projection = { MediaStore.Video.VideoColumns.DATA };
        String orderBy = android.provider.MediaStore.Video.Media.DATE_TAKEN;
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
        ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment