Android: Files that I write to the sdcard do not show in Windows explorer, for Acer Iconiatab

Sending this intent after writing the file solved the issue. Credit goes to Commons Guy, he provided the answer on another forum.

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

Edit: Commons Guy did comment that sending a fake intent (like above) is probably a bad idea, so I used this instead:

   //called after writing file, from my activity 
    new SingleMediaScanner(this, path); 

    private class SingleMediaScanner implements MediaScannerConnectionClient 
    { 
            private MediaScannerConnection mMs; 
            private String path; 
            SingleMediaScanner(Context context, String f) 
            { 
                mPath = f; 
                mMs = new MediaScannerConnection(context, this); 
                mMs.connect(); 
            } 
            @Override 
            public void onMediaScannerConnected() 
            { 
                mMs.scanFile(mFile, null); 
            } 
            @Override 
            public void onScanCompleted(String path, Uri uri) 
            { 
                mMs.disconnect(); 
            } 
        }

On my Acer Iconia Tab, getExternalStorageDirectory actually points to an internal storage (considered as "shared", as opposed to the "private" inner storage), and is mounted as /mnt/sdcard. There is an alternative (and "true") external storage that goes on the SDcard at /mnt/external_sd. I'm not sure if there currently is a perfect method to get that path, though. Do you know which partition is mounted when you connect your Iconia Tab by USB ?


Also found this problem on a Nexus 7. If you device connects to your PC with MTP (the preferred USB connection protocol post honeycomb), when you create a file, the media scanner needs to be updated for it to be immediately visible in Windows Explorer.

Some devices update the media scanner automatically (Samsung S4), some do not (Nexus 7). After creating the new file on the SD card, to make sure the media scanner is updated make the following call.

File file= new File(FullPathAndFileNameToYourNewFile);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);

Here is a really good post on the topic. Thanks go to the author for the explanation and code above. https://www.grokkingandroid.com/adding-files-to-androids-media-library-using-the-mediascanner/

Tags:

Android