0

I want to store a file on the SD card using File. I already achieved to store a file in external storage (named internal storage in my file explorer app. This is NOT the SD card).

I read much about storing data on SD cards and often saw:

new File(Environment.getExternalStorageDirectory() + "/MyFile")

But this will store the file on external storage and not on the SD card. So how can I store a file on the SD card and not on external storage?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JonasPTFL
  • 189
  • 4
  • 15

2 Answers2

1

Ideally, you use the Storage Access Framework (e.g., ACTION_CREATE_DOCUMENT) and let the user choose where the user wants to put this content. The user may wish to put the content somewhere else. For example, the user may not have removable storage.

Temporarily, you are welcome to use getExternalFilesDirs() and similar methods on Context. If these return 2+ elements, the second and subsequent ones are locations on removable storage. On Android 4.4+, your app can write to those locations without any particular permissions. Android Q is changing the rules here a lot, which is another reason to use the Storage Access Framework instead.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • what is the parameter for and what do i have to set there – JonasPTFL May 04 '19 at 12:07
  • @JonasPTFL: I am not certain what parameter you are referring to. If you mean the parameter to `getExternalFilesDirs()`, pass in `null` or one of the `Environment.DIRECTORY_*` values, such as `Environment.DIRECTORY_MUSIC`. See [the documentation](https://developer.android.com/reference/android/content/Context?hl=en#getExternalFilesDirs(java.lang.String)) for more. – CommonsWare May 04 '19 at 12:16
  • Yes, sorry that was my bad! Ok, i passed null as parameter, but the method will return a array of files... What do i have to do with this? – JonasPTFL May 04 '19 at 12:27
  • @JonasPTFL: If it only has one element in the array, the user does not have any removable storage. If the user has 2+ elements in the array, the second and subsequent ones point to app-specific locations on removable storage. You can use those as base directories for creating files, subdirectories, etc. – CommonsWare May 04 '19 at 12:28
  • Ok thank you very much! I achived to store the file on sd card, but in the Android/data/my.app.package/ folder... Can i also store it in main folder, where the Android folder is? – JonasPTFL May 04 '19 at 12:38
  • @JonasPTFL: "Can i also store it in main folder, where the Android folder is?" -- not usually. There is the occasional buggy device that allows this. If you use the Storage Access Framework, the *user* could choose the main folder, in which case you could use the `Uri` you get back from `ACTION_CREATE_DOCUMENT` to write your content there. The overall trend of Android -- particularly with Android Q -- is to give the *user* control over the *user's* device and the *user's* storage. – CommonsWare May 04 '19 at 12:43
-1

Using this may help you:

An answer to Stack Overflow question How do I get the external storage's path?

Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pankaj sharma
  • 181
  • 2
  • 12