3

As a matter of course in my applications, before I read/write anything to the external storage I check Environment.getExternalStorageState()

I'd always assumed it worked as intended in my applications that occasionally write to the storage, but with my most recent application (which is highly dependent on the storage system), I check this as the application starts and I toast an error to the user if something is wrong (state does not equal MEDIA_MOUNTED).

I'm getting a number of reports from users receiving this error toast due to MEDIA_UNMOUNTED.

BUT, there is no removable sdcard to mount/unmount and most puzzling of all, other applications are using the storage regardless.... Which leaves me to conclude they must be testing this another way?

I've only found one post that may be related to the cause, but if you scroll up to the answer above, there are 13 upvotes for a comment reminding users to check the media is mounted correctly first!

So, I'm confused and wondering if someone is aware of an issue?

Community
  • 1
  • 1
brandall
  • 6,094
  • 4
  • 49
  • 103
  • What exactly is the returned string? – 18446744073709551615 Jan 20 '14 at 11:03
  • @18446744073709551615 For the user I'm currently debugging with, it's `MEDIA_UNMOUNTED`. Others have commented on the Play Store, but not replied as to their specific error code – brandall Jan 20 '14 at 11:06
  • isExternalStorageRemovable() ? – 18446744073709551615 Jan 20 '14 at 11:09
  • @18446744073709551615 Not with this particular user. I'm uncertain of the devices that others are experiencing problems with though. – brandall Jan 20 '14 at 11:12
  • OMG. The only thing that comes to my mind is to try{} to check if there are any files under the mount point, and if they are there, and if you can create a file... – 18446744073709551615 Jan 20 '14 at 11:17
  • 1
    Does your application have permissions to access the external storage? IIRC they changed the policy somehow... but i do not remember what exactly. – 18446744073709551615 Jan 20 '14 at 11:20
  • @18446744073709551615 I had considered trying{} to write an empty file to the storage, to double check, but due to the linked answer above it seems as though some devices will also fail on `getExternalStorageDirectory()`, so that doesn't appear fool-proof... Edit: Yes, definitely has the correct permissions. – brandall Jan 20 '14 at 11:20
  • NB: **READ_EXTERNAL_STORAGE** _Added in API level 16_ – 18446744073709551615 Jan 20 '14 at 11:23
  • @18446744073709551615 Thanks - but if you have the permission `WRITE_EXTERNAL_STORAGE` you are automatically granted the read permission. – brandall Jan 20 '14 at 11:25
  • And you believe them? – 18446744073709551615 Jan 20 '14 at 11:27
  • @18446744073709551615 - Yes... http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE – brandall Jan 20 '14 at 11:29
  • You already see their bug. They (the Google) wanted to grant that permission, but they may have failed to do that consistently. In the absence of better ideas, I would try that second permission. – 18446744073709551615 Jan 20 '14 at 11:39
  • @18446744073709551615 - I understand your sceptical logic, but there is nothing in the logcat of the user that shows a permission denial has occurred. More importantly, the call to `getExternalStorageState()` does not require either permission. – brandall Jan 20 '14 at 11:44
  • Hmmm, while searching for something else I find out that http://developer.android.com/reference/android/os/Build.VERSION_CODES.html#KITKAT mentions in the whatsnew list: _The READ_EXTERNAL_STORAGE permission is now always enforced._ and http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE says that the rules depend on the version and are really complex. – 18446744073709551615 Jan 21 '14 at 08:00
  • It is worth pointing out that, even without a removable SD card, the internal storage the device gives out by these APIs can still be mounted to a connected computer instead of the apps. – Lance Nanek May 22 '14 at 13:36

1 Answers1

0

User may forbidden the permission: WRITE_EXTERNAL_STORAGE.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Muse
  • 422
  • 3
  • 7