How can i use Environment.getExternalStorageDirectory()
to read a a stored image from the SD card or is there a better way to do it?

- 29,907
- 37
- 114
- 158

- 1,547
- 3
- 18
- 21
4 Answers
Environment.getExternalStorageDirectory().getAbsolutePath()
Gives you the full path the SDCard. You can then do normal File I/O operations using standard Java.
Here's a simple example for writing a file:
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "myFile.txt";
// Not sure if the / is on the path or not
File f = new File(baseDir + File.separator + fileName);
f.write(...);
f.flush();
f.close();
Edit:
Oops - you wanted an example for reading ...
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "myFile.txt";
// Not sure if the / is on the path or not
File f = new File(baseDir + File.Separator + fileName);
FileInputStream fiStream = new FileInputStream(f);
byte[] bytes;
// You might not get the whole file, lookup File I/O examples for Java
fiStream.read(bytes);
fiStream.close();

- 6,517
- 1
- 30
- 56
-
45Dont forget to check if the external storage is mounted: `Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)` – whlk Oct 11 '11 at 08:19
-
1good comment, Mannaz! BTW, what library do you need to include to use these calls? Is it Android.os.Environment by any chance? – gonzobrains Feb 14 '12 at 00:10
-
@Mannaz and how to proceed when the storage is not mounted? What do I have to do? Which direcory do I have do use to save files? – androidevil May 05 '16 at 23:15
-
@androidevil: There is [a guide in the Android developer docs](http://developer.android.com/guide/topics/data/data-storage.html) in how to use storage. Probably you could then either show the user a message and inform him to mount his external storage or use the internal storage instead. – whlk May 10 '16 at 15:00
-
1baseDir is keep giving me /storage/emulated/0 which is not on SD card. Why do I do? – most venerable sir Jul 13 '18 at 18:48
-
its called external because it is external to app, in reality its the internal memory of the device, not sd card. – satvik choudhary Aug 07 '20 at 19:12
Have in mind though, that getExternalStorageDirectory() is not going to work properly on some phones e.g. my Motorola razr maxx, as it has 2 cards /mnt/sdcard and /mnt/sdcard-ext - for internal and external SD cards respectfully. You will be getting the /mnt/sdcard only reply every time. Google must provide a way to deal with such a situation. As it renders many SD card aware apps (i.e card backup) failing miserably on these phones.

- 1,509
- 15
- 22
-
Same here with Galaxy Tab 2 GT-P3110...I was wondering why my database was not copied to the SD card and I just realised there are 2 cards like you said...how to waste hours of debugging for nothing! – Paranoid Android Oct 07 '12 at 00:11
-
1It's possible that a device using a partition of the internal storage for the external storage may also offer an SD card slot. In this case, the SD card is not part of the external storage and your app cannot access it (the extra storage is intended only for user-provided media that the system scans). – Timuçin Jan 27 '13 at 12:11
-
@Tim Here I am facing problem like this. In Nexus 5 has only built-in storage and Micromax a114 canvas 2.2 has expandable storage.My problem is that when try to download some files from my app not getting download. And the Environment.MEDIA_MOUNTED what it means? – Karthikeyan Ve Jul 02 '14 at 09:40
-
this is solved [android-external-storage-create-new-directory](http://stackoverflow.com/questions/39462667/android-external-storage-create-new-directory-and-then-files-into-it/39462762#39462762) – ELITE Sep 13 '16 at 05:39
-
My images are saved on internal storage, i am using `Environment.getExternalStorageDirectory().getAbsolutePath();` – Moeez Jan 31 '17 at 13:20
As described in Documentation Environment.getExternalStorageDirectory() :
Environment.getExternalStorageDirectory() Return the primary shared/external storage directory.
This is an example of how to use it reading an image :
String fileName = "stored_image.jpg";
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String pathDir = baseDir + "/Android/data/com.mypackage.myapplication/";
File f = new File(pathDir + File.separator + fileName);
if(f.exists()){
Log.d("Application", "The file " + file.getName() + " exists!";
}else{
Log.d("Application", "The file no longer exists!";
}

- 124,308
- 23
- 334
- 268
-
As you mentioned, with `getExternalStorageDirectory()` we can create new directory in user preferred storage (which is generally internal memory). so, what happens if the default storage has been set to `External memory` and it's not **readable/writable**? Does this throw exception? or it will automatically create in `Internal storage`? – S.R Jul 16 '17 at 18:38
To get the directory, you can use the code below:
File cacheDir = new File(Environment.getExternalStorageDirectory() + File.separator + "");

- 7,102
- 69
- 48
- 77