31

I am making an application, which pulls files(Saved by android app) from the device sdcard by adb. The problem is that different devices, have various sdcard directories

i.e:

  • sdcard
  • sdcard/external_sd

Firstly I invented following solution:

  1. run adb shell
  2. ls
  3. Check if dir "sdcard" exists
  4. If yes run sdcard/ ls and check if external_sd exists
  5. return value.

But the problem is that I have two samsung devices GT-I9100 and GT-I9000 and both have a directory sdcard/external_sd. When I am checking System.getenv("EXTERNAL_STORAGE") one returns sdcard and another sdcard/external_sd. I need to pull file which was previously saved to System.getenv("EXTERNAL_STORAGE").

So the question is: is there any command to get sdcard directory directly from adb, without access to Android code?

Or maybe I can start activity with adb's am start, and get return value? Is this possible?

EDIT: Found the solution:

adb shell echo $EXTERNAL_STORAGE
Paweł
  • 363
  • 1
  • 5
  • 13
  • you might want to escape the $ – Blackbelt Feb 21 '12 at 10:26
  • 1
    When I am using `adb shell echo $EXTERNAL_STORAGE` it escape the $ automatically. When I am doing it in this way: 1. `adb shell` 2. `echo $EXTERNAL_STORAGE` I have to end it with `exit` to escape the $ – Paweł Feb 21 '12 at 10:59
  • Duplicate of http://android.stackexchange.com/q/14105/32571 – janot Oct 04 '14 at 06:35

3 Answers3

28

For what its worth - using the $EXTERNAL_STORAGE variable can give you misleading results. I have a HP Slate tablet here, which has the EXTERNAL_STORAGE variable set to /storage/sdcard0 . However when using df (disk free) command on shell, or even the mount command to display free space or mounts, the following becomes obvious:

shell@android:/ $ df
Filesystem             Size   Used   Free   Blksize
/dev                  452.7M  36.0K  452.7M   4096
/mnt/asec             452.7M  0.0 K  452.7M   4096
/mnt/obb              452.7M  0.0 K  452.7M   4096
/system               629.9M  468.5M  161.5M   4096
/data                 5.7 G  2.3 G  3.5 G   4096
/cache                435.9M  16.4M  419.5M   4096
/storage/sdcard0      5.7 G  2.3 G  3.5 G   4096
/mnt/external_sd      29.3G  64.0K  29.3G   32768

so, the external sd card is in fact /mnt/external_sd, instead of the value EXTERNAL_STORAGE returns(which is the internal storage)

John Deaux
  • 381
  • 3
  • 3
25

If I've not misunderstood you, you're looking for something like:

emanuele@Nabucodonosor:~$ adb shell cd \$EXTERNAL_STORAGE
emanuele@Nabucodonosor:~$ adb shell ls \$EXTERNAL_STORAGE
emanuele@Nabucodonosor:~$ adb shell echo \$EXTERNAL_STORAGE
billkw
  • 3,350
  • 3
  • 28
  • 32
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

Its better to use getExternalStorageDirectory() instead of System.getenv("EXTERNAL_STORAGE") This will give you external storage directory irrespective of device.

AndroDev
  • 3,236
  • 8
  • 35
  • 49
  • 1
    irrespective of device ? You mean that System.getenv("EXTERNAL_STORAGE") can not work on some devices? – Paweł Feb 21 '12 at 09:48
  • getExternalStorageDirectory() means external application storage, not external device storage. Google seems to have had a design era where expandable device memory was removed from Android. They are putting it back, but the results are are mess. Post Lollipop, mounting external storage (sdcards, usb, net drives, etc) should be straight forward. – Dominic Cerisano Mar 24 '15 at 23:18
  • I wanted to access sdcard from shell script, this answer helped me a lot. I don't understand why people are voting it down... – Kamran Ahmed Apr 17 '16 at 11:02