2

A third party library out of my control is saving files in:

/storage/emulated/0/Android/data/[packagename]/files

I am aware of the different methods to check storage available on disk for internal and external storage: Android get free size of internal/external memory. What would be the correct method to check storage available for the path above?

Internal, External, or some sort of combination that depends on the device being used and it's storage types. Ie. should adoptable storage be considered in this check?

Alternatively is using the sticky Broadcast of Intent.ACTION_DEVICE_STORAGE_LOW a better solution?

Community
  • 1
  • 1
Dave Thomas
  • 3,667
  • 2
  • 33
  • 41
  • Environment.getExternalStorageDirectory(); appears to return /storage/emulated/0 directory for me. Close but not entirely it. Still researching to try and find a function that would return the path I posted. – Dave Thomas Oct 21 '16 at 07:33
  • So it turns out this is the folder returned by: https://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String) – Dave Thomas Oct 24 '16 at 17:50

2 Answers2

1

Well if you have the path I believe your answer can be found inside How to find the amount of free storage (disk space) left on Android?, check the code samples there.

Community
  • 1
  • 1
Fréderic Cox
  • 321
  • 2
  • 7
  • 22
1

I've selected Fréderic Cox's answer because it is a valid solution. But it is important to also post how we handled this in our code base.

I asked this question on behalf of a colleague. I've just code reviewed his solution and here is a relevant comment from the code about how he handled it:

The 3rd party library is using getExternalFilesDir to determine where to write to. The return value of getExternalFilesDir is the "primary external storage". Contrary to its name, "primary external storage" means the internal storage(!), as it is referring to storage which isn't internal to the OS itself or installed apps but can be used by apps to write to. It will be uninstalled along with the app. Internal, not external. In case you are wondering, an actual external storage, like an SD card, is considered "secondary external storage" (as opposed to the "primary external storage" used here). https://stackoverflow.com/a/29404440

long availableMb = applicationContext.getExternalFilesDir(null).getUsableSpace() / (1024 * 1024);
Community
  • 1
  • 1
Dave Thomas
  • 3,667
  • 2
  • 33
  • 41