public static boolean mkfile(final File file, Context context) throws IOException { if (file == null) return false; if (file.exists()) { // nothing to create. return !file.isDirectory(); } // Try the normal way try { if (file.createNewFile()) { return true; } } catch (IOException e) { e.printStackTrace(); } boolean b = true; // Try with Storage Access Framework. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && FileUtil.isOnExtSdCard(file, context)) { DocumentFile document = getDocumentFile(file.getParentFile(), true, context); // getDocumentFile implicitly creates the directory. try { b = document.createFile(MimeTypes.getMimeType(file), file.getName()) != null; return b; } catch (Exception e) { e.printStackTrace(); return false; } } if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { try { return MediaStoreHack.mkfile(context, file); } catch (Exception e) { return false; } } return false; }