0

I am using below code to fetch all videos from a specific folder.

 String selection=MediaStore.Video.Media.DATA +" like?";
 String[] selectionArgs = new String[]{folderPath};
 return new CursorLoader(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI, COLUMNS_OF_INTEREST, selection, selectionArgs,
                MediaStore.Video.Media.DATE_ADDED + " ASC");

But this doesn't work for programmatically created folders.How can I fetch all video files from a programmatically created folder?

Android Developer
  • 9,157
  • 18
  • 82
  • 139
  • So you create the folder from the app and then try to run the above code? – varunkr Sep 27 '18 at 15:25
  • @varunkr yes.i created folder from the app,added videos to it and then tried running above code – Android Developer Sep 27 '18 at 15:26
  • Make sure the folder is being created on external storage. Since you are providing the EXTERNAL_CONTENT_URI. Another reason can be that you need to scan the sd card for changes. See this https://stackoverflow.com/questions/4646913/android-how-to-use-mediascannerconnection-scanfile/5814533#5814533 – varunkr Sep 27 '18 at 15:32
  • @varunkr I created folder inside Movies directory using `File moviesDir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_MOVIES ); File dir = new File(moviesDir, "XYZ"); ` – Android Developer Sep 27 '18 at 15:37
  • Then you could try scanning for changes as suggested in the answer. I faced a similar issue once although my problem was different than yours. – varunkr Sep 27 '18 at 15:37
  • @varunkr Even for the folder which has been programmatically created months ago,I am not able to fetch videos for it.So I don't think that it can be because recent changes are not detected.That code is for scanning a single file rather than folder.. – Android Developer Sep 27 '18 at 15:42
  • @varunkr Thanks..using MediaScannerConnection works..you can post it as an answer so that i can accept – Android Developer Sep 29 '18 at 11:29
  • Great, I'll add it as an answer – varunkr Sep 29 '18 at 12:46

1 Answers1

1

This is probably happening since your device needs to scan for the changed files. You can do it like this.

 MediaScannerConnection.scanFile(this,
          new String[] { file.toString() }, null,
          new MediaScannerConnection.OnScanCompletedListener() {
      public void onScanCompleted(String path, Uri uri) {
          //Do something
      }
 });
varunkr
  • 5,364
  • 11
  • 50
  • 99