/** * Create a folder. The folder may even be on external SD card for Kitkat. * * @param file The folder to be created. * @return True if creation was successful. */ public static boolean mkdir(final File file, Context context) { if (file == null) return false; if (file.exists()) { // nothing to create. return file.isDirectory(); } // Try the normal way if (file.mkdirs()) { return true; } // Try with Storage Access Framework. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && FileUtil.isOnExtSdCard(file, context)) { DocumentFile document = getDocumentFile(file, true, context); // getDocumentFile implicitly creates the directory. return document.exists(); } // Try the Kitkat workaround. if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { try { return MediaStoreHack.mkdir(context, file); } catch (IOException e) { return false; } } return false; }