3

I tried this:

  public String getFilename() {
    File file = new File(Environment.getExternalStorageDirectory(), "Test2");
    if (!file.exists()) {
        file.mkdirs();
    }
    String uriSting = (file.getAbsolutePath() + "/" + "IMG_" + System.currentTimeMillis() + ".png");
    return uriSting;
}

I tried changing the second line to:

File file = new File("sdcard/Test2");

But still the directory is being created in device storage and not on external sd card.

Bugs Buggy
  • 1,514
  • 19
  • 39
  • Did you provide this permission ``? – Shlomi Haver Jul 07 '16 at 14:11
  • @ShlomiHaver Yes I did, otherwise I wouldn't have been able to write in device storage as well. I remember reading somewhere that even device storage is external storage, it is external to the OS. – Bugs Buggy Jul 07 '16 at 14:14

1 Answers1

3

You do not have arbitrary access to removable storage.

On Android 4.4+, you have two main options:

  1. Use getExternalFilesDirs(), getExternalCacheDirs(), or getExternalMediaDirs() (note the plural), all methods on Context. If they return 2+ locations, the second and subsequent ones are locations on removable storage that your app can read and write, without any permissions.

  2. Use ACTION_OPEN_DOCUMENT and ACTION_CREATE_DOCUMENT, to allow the user to choose where to place some content, where the user can choose removable storage (or Google Drive, or DropBox, or external storage, or anything else). You wind up with a Uri, not a filesystem path; use ContentResolver to work with that Uri to get streams for reading and writing.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I cannot allow the user to do that, I need to create a specific directory and get it's URL. Isn't that possible? – Bugs Buggy Jul 07 '16 at 14:18
  • @AseedUsmani: You cannot create a specific directory, unless that directory happens to be located somewhere under the roots outlined in option #1 above. – CommonsWare Jul 07 '16 at 14:22
  • How does the Camera App saves our clicks on our sd card, under the DCIM/Camera directory? Can I use the same method? – Bugs Buggy Jul 07 '16 at 14:27
  • @AseedUsmani: You already have the code for working with external storage in your question. A device's built-in camera app might have additional rights to work with removable storage, beyond what ordinary apps can do, but unless you are building your own device or own custom ROM, that does not help you. – CommonsWare Jul 07 '16 at 14:28
  • Last word, means what I'm trying is impossible? A specific directory, even if it already exists (say the user himself created it, or chose it the first time) cannot be used to save files every time our App wants? – Bugs Buggy Jul 07 '16 at 14:34
  • @AseedUsmani: "say the user himself created it, or chose it the first time" -- that is what option #2 was for (though since you need a directory, that would be `ACTION_OPEN_DOCUMENT_TREE`, and that is only on Android 5.0+ IIRC). However, if you let the user choose the location, **the user can choose the location**, and that choice does not have involve removable storage. You indicated earlier that you "cannot allow the user to do that", but Android is a consumer operating system, designed to allow users to do what they want. – CommonsWare Jul 07 '16 at 14:38
  • 2
    There's also [Scoped Directory Access](https://developer.android.com/preview/features/scoped-folder-access.html) on API 24 devices that allow you to request read/write access to SD cards. – ianhanniballake Jul 07 '16 at 16:32