Getting Uncaught Handler Exception when trying to copy database to sd card storage direction
I was trying to copy my database file into sdcard. I am using these codes to do it :
private static String DB_PATH = Environment.getExternalStorageDirectory().toString()+"/";
private static final String DATABASE_NAME = "story.sql";
private void copyDataBase() throws IOException {
InputStream myInput = helperContext.getAssets().open(DATABASE_NAME);
String outFileName = DB_PATH + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
When I debug the code exception throws here
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
07-26 14:58:10.689: ERROR/AndroidRuntime(7723): Uncaught handler: thread main exiting due to uncaught exception
07-26 14:58:10.709: ERROR/AndroidRuntime(7723): java.lang.Error: Error copying database
07-26 14:58:10.709: ERROR/AndroidRuntime(7723): at org.android.story.SqliteManager$DatabaseHelper.createDataBase(SqliteManager.java:272)
07-26 14:58:10.709: ERROR/AndroidRuntime(7723): at org.android.story.SqliteManager.open(SqliteManager.java:62)
07-26 14:58:10.709: ERROR/AndroidRuntime(7723): at org.android.story.MainMenu.onCreate(MainMenu.java:21)
I tried on Android OS 2.1 result was same.After I tried on Android 2.3.3 OS version and it worked with charm. Also tried my Android phone which has 1.6 OS version with smaller database(my database was 9.8 MB and changed it with 320 KB size) it worked fine. So why this code works fine with 2.3.3 or a smaller database but not with 1.6 or 2.1? Is there any workaround to use this in 1.6?
EDIT : I write this function that calls copyDataBase()
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
开发者_运维百科}
精彩评论