9

I lost whole day fighting with something trivial, but now I just give up and need your help.

In new Oreo API for reading app code/data/cache/ size we need storage UUID (StorageStatsManager.queryStatsForUid(UUID storageUuid, int uid)).

The storage UUID is possible to fetch from StorageManager.getStorageVolumes() which returns list of all storages which contains also the UUIDs.

And there the problem begins: The UUID from returned list is in String format while StorageStatsManager.queryStatsForUid(UUID storageUuid, int uid) requires UUID object - nothing simplier - there exist method UUID.fromString(String name) which converts it in a while.. but wait, the UUID value from StorageManager list is incompatibile with UUID parser - WHAT?! I've checked on 2 devices (one with Oreo and one with Nougat), both with external memory and for each device I get 2 storages:

  1. Internal device memory with UUID equals "null" - thats fine as this is described in documentation
  2. External card storage with UUID value in format like "4012-1A4B" what is invalid UUID format and causes UUID.fromString throws IllegalArgumentException.

Can anyone explain me what am I doing wrong and how should I get UUID for external storage? Or just new API StorageStatsManager.queryStatsForUid(UUID storageUuid, int uid) is useless as we are unable to get storage UUID?


FYI:

Whats more there is another StackOverflow question where usage of this API is marked as correct solution: https://stackoverflow.com/a/44708209/1946366 what makes me even more frustrated...

peemes
  • 165
  • 1
  • 7
  • Try https://stackoverflow.com/a/44837112/115145 – CommonsWare Feb 02 '18 at 18:59
  • The same problem. The output is: java.lang.IllegalArgumentException: Invalid UUID string: 70DA-3A8D at java.util.UUID.fromString(UUID.java:194) at android.os.storage.StorageManager.convert(StorageManager.java:2091) at android.os.storage.StorageManager.getUuidForPath(StorageManager.java:763) so this method return also invalid UUID. – peemes Feb 07 '18 at 13:27
  • 1
    did you find the solution for it? – sam_k May 15 '18 at 00:17

3 Answers3

7

Actually the fromString(uuid_string) takes a "long" UUID (with like 4 or 8 parts can't remember exactly).

However the following works perfectly:

UUID.nameUUIDFromBytes(sv.getUuid().getBytes());
3c71
  • 4,313
  • 31
  • 43
2

I found that this is raised already bug on Android: https://issuetracker.google.com/issues/62982912 so I think that my questions is unanswerable.

peemes
  • 165
  • 1
  • 7
  • I read through the chain of 3 related issues starting with the one you linked. It does look like a platform issue. However! It seems strange and I was skeptical about it, but the answer from @3c71 really seems to work. Go figure :-) – Mike Hardy Nov 30 '22 at 01:54
2

I think the application Info of each package has Storage Volume UUID on which this application is being hosted.

You can get it by ,

ApplicationInfo ai = context.getPackageManager().getApplicationInfo(packagename, 0);
StorageStats storageStats = storageStatsManager.queryStatsForUid(ai.storageUuid, uid);

This is introduced in Oreo Version

Jyoti JK
  • 2,141
  • 1
  • 17
  • 40