tomsv
tomsv

Reputation: 7277

When does getExternalStorageDirectory().getPath() return something else than "/sdcard/"?

I get a warning from android lint:

Do not hardcode "/sdcard/"; use Environment.getExternalStorageDirectory().getPath() instead

I will fix this, but still would need to know:

On which (example) devices is this a problem, what other (example) paths can you get from this call? If this is not related to specific devices, to what is it specific or when would it happen?

Under what circumstances is there no /sdcard/ directory that my app could write to?

The app has the rights

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Upvotes: 0

Views: 2930

Answers (3)

Anggrayudi H
Anggrayudi H

Reputation: 15155

The SD card path is different for different Android manufacturers. So I make my own research with my friends' sdcard on their phone. And the result:

Sony XPERIA X10i (my phone)

  • Android 2.3.3 (Gingerbread)
  • path for phone internal memory: not available
  • path for sdcard: /mnt/sdcard/

Samsung Galaxy S3 Mini & Samsung Galaxy Young (given same result)

  • Android 4.2.2 (JellyBean)
  • path for phone internal memory: /storage/sdcard0/
  • path for sdcard: /storage/extSdCard/

OPPO (I don't remember what her phone type is)

  • Android 4.2.2 (JellyBean)
  • path for phone internal memory: /storage/sdcard0/external_sd/
  • path for sdcard: /sdcard0/

I wrote the result in a book. So my suggestion, never use hardcode for sdcard's path. Check here to know your sdcard's path.

Upvotes: 1

user2139223
user2139223

Reputation: 5

The external storage may point other places,

you may define and mount other place as device external storage,

it a configure option, that why you have a Lint warning.

For example if the device support External SD card, than the external sdcard (getExteranlStorage()) will point to him (the exteranl sdcard will mount at /nmt/sdcard#/), top keep the internal sdcard free.

To make it clear

The /sdcard/ == /mnt/sdcard[0] -> internal sdcard

other sdcard will mount at /mnt/sdcard[1..]/ -> external sdcard

And also the manufacture of the device can call it as he wish (/sdcard/ is just a convention not a must have)

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006624

what other (example) paths can you get from this call?

Partly, it is whatever the manufacturer wants.

Partly, it will vary based on the user account that is running your app. Please bear in mind that for a few years (since the release of Android 4.2), Android devices can have multiple user accounts (tablets starting with 4.2, phones starting with 5.0). Each user gets a distinct location for internal and external storage, and there is no guarantee as to what actual filesystem paths those will point to.

NEVER HARDCODE ROOT PATHS to internal or external storage. Always use an appropriate method for getting a root location, then use the appropriate File constructor to point to whatever you want within there.

The app has the rights

Since there is no WRITE_INTERNAL_STORAGE permission in Android, please remove it.

Upvotes: 2

Related Questions