Android how to use Environment.getExternalStorageDirectory()
Asked Answered
S

4

59

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?

Seize answered 28/3, 2011 at 1:6 Comment(0)
C
90
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();
Checkrein answered 28/3, 2011 at 1:17 Comment(6)
Dont forget to check if the external storage is mounted: Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)Wary
good comment, Mannaz! BTW, what library do you need to include to use these calls? Is it Android.os.Environment by any chance?Pivotal
@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?Tirzah
@androidevil: There is a guide in the Android developer docs 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.Wary
baseDir is keep giving me /storage/emulated/0 which is not on SD card. Why do I do?Stanger
its called external because it is external to app, in reality its the internal memory of the device, not sd card.Future
N
39

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.

Notus answered 23/7, 2012 at 19:23 Comment(5)
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!Deli
It'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).Esdras
@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?Countercharge
this is solved android-external-storage-create-new-directoryFashoda
My images are saved on internal storage, i am using Environment.getExternalStorageDirectory().getAbsolutePath();Miner
C
2

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!";
         }
Claytor answered 2/12, 2016 at 1:0 Comment(1)
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?Gallopade
U
0

To get the directory, you can use the code below:

File cacheDir = new File(Environment.getExternalStorageDirectory() + File.separator + "");
Undertrick answered 16/6, 2020 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.