Android 在 SD 卡上创建目录

Android directory creation on SD card

我正在使用 File.mkdirs() 在我的 SD 卡上创建一个目录。 mkdirs() returns 值为真,当我读取目录下一次执行时,目录就在那里。但是,我无法从我的 PC 或内置文件浏览器中看到该目录。

Android SDK 版本为25. 我已成功请求WRITE_EXTERNAL_STORAGE.

权限

为什么我在电脑上看不到目录?

我的代码:

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());
}

来自日志:

第一次执行:

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

后续执行:

D/BLC: onCreateOptionsMenu: EXISTS

在 Android 设备中创建目录后,您需要刷新媒体以便您的文件夹在 PC 中可见(windows) 所以在你完成制作目录后,使用目录路径调用以下方法。

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);
}