0

If I'm constantly writing to a file located either in internal memory or SD card, there's a possibility that at some point there will be no more free space. This could throw an exception and create a corrupt file, depending on the way the file is written.

Can Android notify that it is running out of space, so that I can gracefully close the file?

The ACTION_DEVICE_STORAGE_LOW intent does not work for the SD card as far as I know.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Petrakeas
  • 1,521
  • 15
  • 28
  • Seems to be a duplicate of http://stackoverflow.com/questions/3394765/how-to-check-available-space-on-android-device-on-mini-sd-card – Actiwitty Nov 24 '15 at 23:34
  • In my case, I am constantly writing to this file. So, I would either have to check for the available space from time to time or count how many bytes I have written. I was hopping for a system callback if it was possible. But I guess, there's no other way. – Petrakeas Nov 25 '15 at 01:05
  • @Petrakeas can you resolve it? – pengwang Jan 09 '21 at 08:55

1 Answers1

1

Two ways to achieve this.

1) Check remaining memory in storage(there are many answers for this, just search), and decide if you want to create file or delete it. This is Recommended to check the storage before creating new files.

2) Have a subclass of Application class in your app, there is this method named onLowMemory which will let your application know when system is running on low memory.

Community
  • 1
  • 1
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • I don't know the size of the file beforehand. It's increasing as time goes by. I could check the remaining size before starting to stream/write to it and count how may bytes I have written since then. onLowMemory() is not related to storage. – Petrakeas Nov 25 '15 at 01:02
  • Only option seems to check size and have it in some variable and write only based on the remaining size value. – AAnkit Nov 25 '15 at 07:17