public static OutputStream getOutputStream(@NonNull final File target, Context context, long s) { OutputStream outStream = null; try { // First try the normal way if (isWritable(target)) { // standard way outStream = new FileOutputStream(target); } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Storage Access Framework DocumentFile targetDocument = getDocumentFile(target, false, context); outStream = context.getContentResolver().openOutputStream(targetDocument.getUri()); } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { // Workaround for Kitkat ext SD card return MediaStoreHack.getOutputStream(context, target, s); } } } catch (Exception e) { Log.e("AmazeFileUtils", "Error when copying file from " + target.getAbsolutePath(), e); } return outStream; }