简体   繁体   中英

How to check wether the SD card is corrupted on Android

Here is the scenario:

I'm saving data from network to local storage. And there are occasions that the SD card is partially corrupted.

There are several methods that I could come out with:

IOException

Assuming an IOException will be thrown when write or flush on corrupted blocks. This is not tested yet since I don't have a corrupted SD card. So this one is a hypothesis.

Comparing the size

After every flush I compare the size of written buffer with actual file. I think is more practical despite its inelegance.

Checksum

This method is not reliable because there're two factors that would cause a corrupted file: error on network or SD card. So I can't point out which is the real cause.

Any suggestion will be appreciated! Thanks in advance.

Update :

Sorry for the obscure. I read data from HttpEntity.getContent() write to a RandomAccessFile . The underlying protocol of HttpEntity.getContent() is TCP.

You could try to get the sd cards state

Boolean Accessable = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

if(Accessable)
{
  // Sd card is accessable, write data
}
else
{
 // probably corrupted or removed 
}

If you are using TCP, networking errors will be taken care of. TCP protocol does retransmit/abort transparent to the app developer. You will get IOException if it can't receive the data correctly. If you are not using TCP, then you can build the logic to have some validation done by using checksums. Once the networking layer completes its error checking, then you can have your SD card error logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM