Venkatesh
Venkatesh

Reputation: 200

Does File.mkdirs() need WRITE_EXTERNAL_STORAGE permission

Does below code need WRITE_EXTERNAL_STORAGE Permission? I mean does file.mkdirs(); need WRITE_EXTERNAL_STORAGE to write file

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.ENGLISH);
    filename = sdf.format(new Date());

    try {
        String path = ctx.getApplicationContext().getFilesDir().getPath();
        OutputStream fOut = null;
        File file = new File(path, "thinkly");
        if (!file.exists()) {
            file.mkdirs();
        }

        File file2 = new File(file, filename + ".jpg");
        fOut = new FileOutputStream(file2);

        capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fOut);
        fOut.flush();
        fOut.close();

        return file2.getAbsolutePath();

    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }

Upvotes: 0

Views: 350

Answers (1)

Guy4444
Guy4444

Reputation: 1649

All Android devices have two file storage options: internal storage and external storage.

internal storage:

  • Files saved here are accessible by only your app.
  • When the user uninstalls your app, the system removes all your app's files from internal storage.

External storage:

  • It's world-readable, so files saved here may be read outside of your control.
  • When the user uninstalls your app, the system removes your app's files from here only if you save them in the directory from getExternalFilesDir().

These are the common file access functions in Android, so you can know what needs permission and what doesn't:

  • getFilesDir() - No permission is required
  • getCacheDir() - No permission is required
  • getExternalFilesDirs() - Permission is required
  • Environment.getExternalStorageDirectory() - Permission is required

In conclusion

In your code, you use the getFilesDir() method - so you do not need to ask for permission.

extrad:

External storage permissions:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Upvotes: 2

Related Questions