SQLiteDiskIOException with error code 10: disk I/O error

yava picture yava · Aug 10, 2011 · Viewed 11.5k times · Source

I moved an SQLite database to an SD card, but sometimes it will throw the following exception:

android.database.sqlite.SQLiteDiskIOException: error code 10: disk I/O error

Any suggestions on how I can prevent this from happening?

Answer

escape-llc picture escape-llc · Oct 21, 2011

Since you are using External Storage, your application must be aware of the External Storage state, which you can get via Environment.getExternalStorageState().

You must only access External Storage when the state is MOUNTED.

If you are trying to open a file (or SQLite DB; it's just a file!) during device startup, you will definitely have problems, because External Storage is not mounted until well into device startup (after Home Screen is displayed).

The way out of this is to register an IntentFilter to be notified of all changes in External Storage state, and behave accordingly.

It's always a good idea to read the documentation! See the Android documentation for details: http://developer.android.com/reference/android/os/Environment.html

Using External Storage for this purpose is dubious, because you cannot control when it becomes unavailable! For example, the user plugs their device into USB on their computer, and selects the "Enable USB Mass Storage" option, so they can access their device's External Storage from their computer. Bad news! Your app will be disconnected from External Storage, and most-likely without the opportunity to do any clean-up IO!

With this state of affairs, you are now left with What To Do with all the data you would be putting into your database, if only it were available.