2

I am downloading files and saving them to SDCard and want to capture a event when SDCard is full. Right now it throws IOException if no memory is left on SDCard.

How to distinguish the memory full exception then the other IOExceptions?

Cheers, Prateek

Prateek Jain
  • 1,234
  • 14
  • 24

3 Answers3

1

If there is no such thing like NoMemoryLeftExeption then usually by the message.


You can check after the IOException space left on the SD Card... if more then some small amount then rethrow that exception else you can assume that this is MemoryFullException

0

If you know how much data you are writing to the SD card, you could check whether there is sufficient space before you try to write and thus avoid the IOException altogether.

Dan Dyer
  • 53,737
  • 19
  • 129
  • 165
  • 1
    Yes, I know how much data I am going to write. But what if any other source is also writing on SDCard at the same time? Say images are being clicked & written on the SDCard at the same time. – Prateek Jain Dec 27 '10 at 18:10
  • @Prateek OK, you can also do the check after catching the IOException. But if there is concurrent writing/deleting of data you will never be 100% certain. – Dan Dyer Dec 27 '10 at 18:14
  • Thanks.. Will it be Ok if I compare the detailMessage of IOException which is "No space left on device" in case of SDCard full to identify such situations? I am not sure if this is a good practice. – Prateek Jain Dec 27 '10 at 18:20
  • @Prateek It might work for now, but there is no guarantee that that message won't be changed in future. – Dan Dyer Dec 27 '10 at 18:22
-1

If the IOException message (e.getMessage()) contains the following text

ENOSPC (No space left on device)

you can be 100% sure the sd card is full.

Ceetn
  • 2,728
  • 2
  • 25
  • 28