10

I want to download some files and save them into the internal storage of the phone/tab. Tried on Samsung Galaxy Note 2 and Galaxt Tab 10.1. When I use /storage/sdcard0/ on them the code runs successfully but when I use Galaxy Nexus 3 the code fails.

I want to get absolute path of the phoe or tab's internal storage' Absolute Path.

Does it possible, if so how?

Erdinç Özdemir
  • 1,363
  • 4
  • 24
  • 52

3 Answers3

32

When I use /storage/sdcard0/ on them the code runs successfully but when I use Galaxy Nexus 3 the code fails.

/storage/sdcard0/ is not internal storage. It is external storage.

(BTW, since there is no device named "Galaxy Nexus 3", I am assuming that you simply meant "Galaxy Nexus")

To find locations on internal storage for your app, use getFilesDir(), called on any Context (such as your Activity, to get a File object.

To get a location on external storage unique for your app, use getExternalFilesDir(), called on any Context (such as your Activity, to get a File object.

To get a standard location on external storage for common types of files (e.g., movies), use getExternalStoragePublicDirectory() on Environment.

To get the root of external storage, use getExternalStorageDirectory() on Environment. This, however, is considered sloppy programming nowadays, as it encourages developers to just put files in random locations.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm new on android programming. I save files into the storage. For example when I look at on my Galaxy Note 2's /storage/sdcard0/ I see Whatssapp, Music, Alarm sth.. How can I get /storage/sdcard0/ path for all phones/tabs. Thanks. – Erdinç Özdemir Jul 09 '13 at 11:30
  • 2
    @ErdinçÖzdemir: use `getExternalStorageDirectory()` on `Environment`. This, however, is considered sloppy programming nowadays, as it encourages developers to just put files in random locations. Also, the *specific* path that represents external storage varies by manufacturer and OS version. That's why you use methods, rather than hard-coded strings, to find locations on external storage. – CommonsWare Jul 09 '13 at 11:42
  • @CommonsWare: I have internal storage on my LG optimus device and it points internal storage to **/storage/sdcard0/**, i checked it through file manager....so it bit confusing whether sdcard0 point to internal or external – Shashank Nov 16 '13 at 14:28
  • @shihon: "I have internal storage on my LG optimus device" -- you have internal and external storage on an LG Optimius, unless you broke it with a hammer or something, in which case you have neither. Please read the documentation for what the definition is of "external storage". – CommonsWare Nov 16 '13 at 14:31
  • 4
    @CommonsWare: Why should i broke my device with a hammer...i know definition of external storage,...anyway thanks for you hammer support – Shashank Nov 16 '13 at 14:53
10

why can't you use Environment.getExternalStorageDirectory() for getting /storage/sdcard0/ path from code ?

need to add permissions as well

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Desu
  • 464
  • 4
  • 6
1

The solution found is 1st edit the emulator:

  1. Go to Android Virtual Device Manager and then edit the emulator
  2. Set say 100 MB for SD d card for the respected emulator, say Ok
  3. Save and close emulator and start

It worked for me, the app is no longer giving an error and is working fine.

madmaskd
  • 11
  • 3