Reputation: 97
I know this may have been answered many times and I have searched, but I can't resolve my problem. Every time I try to write to sdcard, it appears to be going to internal storage. My lines of code are simply
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() +"/mydir");
dir.mkdirs();
When I go to My Files, the directory always appears in /storage/emulated/0 and not on the sdcard. I have all the permissions set in the manifest. Is there something simple I'm missing?
P.S. My device is the Note 3. I am able to copy files to the sdcard manually.
Upvotes: 4
Views: 13736
Reputation: 157
Print out
dir.toString()
above and see what you get. Then compare it with
File dir = new File(getExternalFilesDir(null) + File.separator + "mydir");
If you do like the 2nd alternative, you should get (I did anyway) /storage/sdcard0/data/your.package.name/files/mydir and your directory above is mydir, not My Files?
Upvotes: 0
Reputation: 3522
Here I posted the code for finding the sdcard path (in the question's body)
How can I properly obtain the external SD card path?
It should solve your problem. The "external" storage is not always your sdcard, because many Android devices consider as external a partition of the internal memory. The path of the real external storage (your sdcard) is setted by the device's constructor, and there's not a standard API for obtaining it.
Upvotes: 0
Reputation: 1006614
How to write files to external storage (sdcard)
External storage is not an "sdcard" on most Android devices.
When I go to My Files, the directory always appears in /storage/emulated/0 and not on the sdcard
That is because /storage/emulated/0
is external storage.
Quoting the documentation:
Every Android-compatible device supports a shared "external storage" that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage.
There is almost no support in the Android SDK for alternative storage locations, such as an SD card. Android 4.4 has a bit, as is described on the same documentation page:
Sometimes, a device that has allocated a partition of the internal memory for use as the external storage may also offer an SD card slot. When such a device is running Android 4.3 and lower, the getExternalFilesDir() method provides access to only the internal partition and your app cannot read or write to the SD card. Beginning with Android 4.4, however, you can access both locations by calling getExternalFilesDirs(), which returns a File array with entries each location. The first entry in the array is considered the primary external storage and you should use that location unless it's full or unavailable. If you'd like to access both possible locations while also supporting Android 4.3 and lower, use the support library's static method, ContextCompat.getExternalFilesDirs(). This also returns a File array, but always includes only one entry on Android 4.3 and lower.
Upvotes: 6
Reputation: 135
You will need to find the external sd card manually, this depends on the phone.
See this tutorial where there is an example of how to test the different paths to the sd card.
Upvotes: -1