Is it possible to move the internal DB to the SDCard?

前端 未结 4 1651
星月不相逢
星月不相逢 2020-12-09 00:10

As the database in my app grows, it is going to require more and more of the internal phone space. There isn\'t any sensitive/private data in the DB, so I\'m interested in m

4条回答
  •  死守一世寂寞
    2020-12-09 00:55

    I found I could use a full path in Android 2.2, but in 2.1 the Context.openOrCreateDatabase() method threw an exception. To work around this I wrapped that method to call SQLiteDatabase.openOrCreateDatabase() directly. Here is the constructor for my extended SQLOpenHelper

    public class Database extends SQLiteOpenHelper {
      public Database(Context context) {
        super(new ContextWrapper(context) {
            @Override public SQLiteDatabase openOrCreateDatabase(String name, 
                    int mode, SQLiteDatabase.CursorFactory factory) {
    
                // allow database directory to be specified
                File dir = new File(DIR);
                if(!dir.exists()) {
                    dir.mkdirs();
                }
                return SQLiteDatabase.openDatabase(DIR + "/" + NAME, null,
                    SQLiteDatabase.CREATE_IF_NECESSARY);
            }
        }, NAME, null, VERSION);
        this.context = context;
      }
    }
    

提交回复
热议问题