Search code examples
androidandroid-sdcard

Android directory creation on SD card


I am using File.mkdirs() to make a directory on my SD card. mkdirs() returns a value of true, and when I read the directory the next execution the directory is there. However, I cannot see the directory from my PC, or from the built-in file browser.

Android SDK version is 25. I have successfully requested permission for WRITE_EXTERNAL_STORAGE.

Why am I not able to see the directory from the PC?

My code:

File resDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "BLC/res/" + mSpeciesID);
if (resDir.exists()) {
    Log.d(TAG, "onCreateOptionsMenu: EXISTS");
} else {
    if (!resDir.mkdirs()) {
        Log.d(TAG, "onCreateOptionsMenu: mkdirs FAILURE ");
    };
    Log.d(TAG, "onCreateOptionsMenu: "+ resDir.getAbsolutePath());
}

From the log:

First execution:

D/BLC: onCreateOptionsMenu: /storage/emulated/0/Documents/BLC/res/206

Subsequent executions:

D/BLC: onCreateOptionsMenu: EXISTS

Solution

  • After creating a directory in Android device, you need to refresh your media so that your folder get visible in PC(windows) so after you are done with making the directory call the following method with directory path.

    public static void refreshSystemMediaScanDataBase(Context context, String docPath)
    {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri contentUri = Uri.fromFile(new File(docPath));
        mediaScanIntent.setData(contentUri);
        context.sendBroadcast(mediaScanIntent);
    }