morristech
3/10/2017 - 6:01 AM

app specific directory location

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