Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getExternalStorageDirectory not working

I am trying to save a file to my SDcard on my Samsung Galaxy Nexus running 4.2.2. From my app I am using

Environment.getExternalStorageDirectory() 

But it returns

/storage/emulated/0/ 

Which is not where my SDcard information is located to. Is there any working method I can use to get the correct path to my SDcard?

like image 963
Luke Pring Avatar asked Mar 19 '13 13:03

Luke Pring


People also ask

Where is getExternalFilesDir?

It returns the path to files folder inside Android/data/data/your_package/ on your SD card. It is used to store any required files for your app (e.g. images downloaded from web or cache files).

How do I save files on Android?

Right-click the filename, and a menu pops up that allows you to perform different operations on the file. Seeing the File in Device File Explorer. Open lets you open the file in Android Studio. Save As… lets you save the file to your file system.


2 Answers

Actually, that is the correct location.

From android 4,2 onwards, Google introduced multiple user accounts. Every user has his/her own external storage, which has the user ID in the path to maintain uniqueness.

The primary (default) user's ID is 0. So you get /storage/emulated/0/ as the path to the external storage.

like image 100
Raghav Sood Avatar answered Sep 22 '22 05:09

Raghav Sood


I just learned this is the Jelly Bean's way of dealing with the lack of android.permission.WRITE_EXTERNAL_STORAGE permission. I haven't seen such a behavior with older versions of Android.

Just add this line to your AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
like image 27
ohaleck Avatar answered Sep 23 '22 05:09

ohaleck