0

I am writing a media player application. I am showing all videos of my sd card in a grid view. It's working fine. If any one making changes to those videos(like rename, delete) using some other app like file expert or file manager are not not effecting my grid view. My grid still showing old names and deleted files. To resolve this i have added refresh button. In this i am using

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStorageDirectory())));

But this is ot supported in KITKAT version. Then after google, i got this solution Android How to use MediaScannerConnection scanFile But i don't know how to scan my sdcard with MediaScannerConnection on my refresh buttton click.

Community
  • 1
  • 1
saa
  • 1,538
  • 2
  • 17
  • 35

1 Answers1

1

You can use the scanFile methof from the MediaScannerConnection

For example, to refresh files in a specific directory, you can call:

for (File child : fileFolder.listFiles()) {
    if (child.isFile()) {
        fName = child.getName();

    MediaScannerConnection.scanFile( MyActivity.this,
        new String[] { "folderPath" + fName },
        null,  new MediaScannerConnection.OnScanCompletedListener() {
              public void onScanCompleted(String path, Uri uri) {
                 //file scanned
              }
        });
    }
}

View source here.

Hope this helped!

gilonm
  • 1,829
  • 1
  • 12
  • 22
  • Hi gilonm. How can i run media scanner service programatically. So that i can have recent files in my grid view – saa Feb 20 '14 at 10:16
  • You posted that you have a refresh button. So, 1) you can put this code (modified to your needs) in the onClick on the Button, or 2) You wrote that changes are made to the videos - after every change just put this code and it will refresh – gilonm Feb 20 '14 at 10:22
  • My app will not make any changes. flow is like this. 1.load video files to my app. --> kill my app. --> open file expert --> rename some video file --> kill file expert --> now open my app --> my app still shows old name bot not new name. I wanna solve this problem – saa Feb 20 '14 at 10:34
  • Call `notifyDataSetChanged()` to refresh your GridView once you have done the scanning done by `MediaScannerConnection` – gilonm Feb 20 '14 at 10:41
  • Yeah.. that i can do. But I dont know how to use this MediaScannerConnection API. And i wanna scan whole SD CArd to get new video files if they present, etc – saa Feb 20 '14 at 10:44
  • Where are the videos coming from? Or to be exact - are they all getting put in one folder? If so, like I wrote, you call the MediaScanner on that folder where all videos are stored and then it will refresh the video content. After that, call the notifyDataSetChanged() to refresh your GridView. – gilonm Feb 20 '14 at 11:05
  • Ok... I am getting videos from sdcard not from one folder. videoCursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, sorterOrder); – saa Feb 20 '14 at 11:34
  • If I understand correctly, your videoCursor returns a Cursor with all videos found in "proj". If so, don't those videos have a path where they are stored? If so, using the Cursor you can retrieve the actual path where each video is stored, and then run the MediaScanner on each result from Cursor – gilonm Feb 20 '14 at 11:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47941/discussion-between-saa-and-gilonm) – saa Feb 20 '14 at 12:01