2

I want to give the user the option to save images on their sd card and I want to be able to calculate the sizes of folders of such images. I find that Environment.getExternalStorageDirectory() returns internal storage. The only way I can find the sd card is by using an ACTION_OPEN_DOCUMENT_TREE request. But this means I have to use DocumentFiles rather than regular Files. I can only get a content uri, not a direct path to the sd card itself.

So, File.length() works well with internal storage but DocumentFile.length() is extremely slow with external storage. Is there any way to get the size of a folder based on its content uri without it taking forever? Or can I somehow find an actual path to the sd card and use Files instead? I found a folder like /storage/3857-3732 but I don't seem to have permission to write to it (yes I have WRITE_EXTERNAL_STORAGE permission).

karnok
  • 1,202
  • 1
  • 12
  • 17
  • `isFile()` and `isDirectory()` work fine (true when appropriate). I got `android.support.v4.provider.TreeDocumentFile` for both files and folders. – karnok Feb 09 '18 at 08:51
  • What does "extremely slow" mean? Compared to what? Does it take 200ms per call to `length()`? 2 seconds per call? Did you observe, that calculating total size takes N seconds for directory with N nested items, but K – user1643723 Feb 12 '18 at 06:26
  • In case someone comes up here still looking for a solution, I built a wrapper over this with some pretty good performance. You can check the wrapper & performance info. here: https://github.com/ItzNotABug/DocumentFileCompat – Darshan Dec 05 '21 at 17:29

0 Answers0