0

I used below query to load all pictures or video from sd card and it works accordingly. But when I add some video or pictures manually into sd card at different folder then its not loading after query. Please suggest me regarding same.

    final String[] columns = { MediaStore.Video.Media.DATA,
                MediaStore.Video.Media._ID};


        final String orderBy = MediaStore.Video.Media.DATE_TAKEN;
        Uri videosuri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

        Cursor imagecursor = getContentResolver().query(videosuri, columns, null, null, orderBy);


        if (imagecursor != null && imagecursor.getCount() > 0) {

            while (imagecursor.moveToNext()) {

                int video_id=imagecursor.getInt(imagecursor.getColumnIndex(MediaStore.Video.Media._ID));
                int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Video.Media.DATA);
 String path=imagecursor.getString(dataColumnIndex);



            }
        }
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50

1 Answers1

1

When you "add some video or pictures manually into sd card at different folder", the MediaStore has to be updated in order for them to be available to your query, as well as to other apps that use the MediaStore backend.

Adding them via MTP or apps like Gallery will invoke the MediaScanner (or some similar process) to add them to the MediaStore, but adding them via adb push or in your own code requires you to explicitly do so afterward.

In your code, after writing an image or video file to the SDCard, you can pass the path and MIME type of the file to the MediaScanner by implementing the MediaScannerConnectionClient interface in a class, instantiating it, then calling scan(). The MediaScanner will then open the file, collect/generate metadata, and add the file to the MediaStore

See android - dynamically add pictures to gallery widget for an example class using this approach.

Community
  • 1
  • 1
unrulygnu
  • 1,596
  • 13
  • 16
  • Ok means files will come in query after it complete the scanning process which is doing internally right. I am not passing vai code manually inserting files into sd card – Sunil Kumar Feb 18 '15 at 09:54
  • Either you need to trigger a media scan on each specific file in code as in the link in my answer above, or a full media scan of the shared storage must be run so the system can find the new files and add them to the `MediaStore`. As far as I know, the only time the system performs this media scan automatically is when the storage partition is mounted; for internal storage this means when the system is booted. So if you do not trigger your own media scan, you may have to wait until either the device is rebooted, or some other process somehow forces a full media scan of shared storage. – unrulygnu Feb 18 '15 at 14:31
  • But how my app know that user added some image/video manually in sd card and when run this media scan and where? – Sunil Kumar Feb 18 '15 at 14:31
  • If the user added it via a method that triggers a media scan, like via MTP or apps like Gallery, then it will be available almost immediately to your query. If they add it via a method that does _not_ trigger a media scan, like `adb push` or an app that doesn't invoke the `MediaScanner`, then the image/video will not be available to your query until a full media scan occurs. – unrulygnu Feb 18 '15 at 14:33
  • NO user added in sd card via usb cable connect with mobile to computer means just they copied from other place and paste in side the sd card. Then How I can check and make media scan – Sunil Kumar Feb 18 '15 at 14:43
  • If the files are being copied from the computer to the mobile device via MTP, then the files should be scanned and added to the `MediaStore` immediately after they're written. If the files are being copied via USB Mass Storage when connected to the computer, then a full scan should be triggered when it's dismounted from the computer and remounted by the mobile device. If the files are being copied via `adb push`, then the media scan will take place only when the shared storage is remounted (on reboot if internal storage) or if some other process forces a full media scan. Hope this helps. – unrulygnu Feb 18 '15 at 15:09