Skip to content

Instantly share code, notes, and snippets.

@snadjafi
Created June 10, 2014 00:29
Show Gist options
  • Select an option

  • Save snadjafi/2bbc12c40bad159e935c to your computer and use it in GitHub Desktop.

Select an option

Save snadjafi/2bbc12c40bad159e935c to your computer and use it in GitHub Desktop.
app specific directory location
public static File createExternalStoragePrivateFile(Context context, byte[] bytes, String fileName) {
deleteExternalStoragePrivateFile(context, fileName);
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), fileName);
try {
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
Timber.w(e, "ExternalStorage", "Error writing " + file + e.getMessage());
return null;
}
return file;
}
public static void deleteExternalStoragePrivateFile(Context context, String fileName) {
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), fileName);
if (file != null) {
file.delete();
}
}
public static boolean hasExternalStoragePrivateFile(Context context, String fileName) {
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), fileName);
if (file != null) {
return file.exists();
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment