I have written create database code;I get ERROR/AndroidRuntime(7193): at com.android.xont.db.DataBaseHelper.createDataBase(DataBaseHelper.java:43)
This is my code :
public class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.android.xont.controller/databases/";
private static String DB_NAME = "DEMOUserVentura_HEMA.db";
private SQLiteDatabase DEMO开发者_如何学CUserVentura_HEMA;
private final Context myContext;
private DataBaseHelper myDbHelper;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
**/
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase().close();
//this.getReadableDatabase().getPath();
//System.out.println( " ===== " + this.getReadableDatabase().getPath());
try {
copyDataBase();
} catch (IOException e) {
throw new Error(e);
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
DEMOUserVentura_HEMA = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (DEMOUserVentura_HEMA != null)
DEMOUserVentura_HEMA.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
When I call this activity I get an exception. My database size is : 4.55MB.
When I print getReadableDatabase().getPath() that is return db path correctly.
Please help me out. I've been pulling my hair out one for a week.
When I run through the emulator, it copy small amount of the file. Not all. then I did manual way copy.
Now the problem is HTC phone. How to fix that Exception. please help me.
Thanks in advance..
The AssetManager has file size limitations. On some devices asset files larger than 1MB cannot be opened. You have to split the file in smaller chunks, put those in your asset folder and reassemble the chunks when your app is running.
See this question on how to reassemble the chunks.
To create the chunks on OSX or Linux you can use the commandline tool 'split'.
See here or here or use google with keywords 'android databasehelper asset' to find tutorials on how to implement a DatabaseHelper that reassembles the chunks from the asset folder when the db is openend the very first time.
精彩评论