1
   @Override
    public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory){
         File externalFilesDir = Environment.getExternalStorageDirectory();
         if(externalFilesDir == null) 
                return null;
         File dbFile = new File(externalFilesDir, SQLiteHelper.DATABASE_NAME);
         return SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), null, SQLiteDatabase.CREATE_IF_NECESSARY);
    }

我使用上面的代码将我的 db 文件写入外部存储。所以它保存在/mnt/sdcard/. 当我在 Android 2.3.5 中尝试它时,它可以工作。但是对于 Android 4,它不起作用。但它仍然没有给我任何错误。

4

1 回答 1

2

这是寻找相同事物的其他人的答案。

 // ======= This will allow to create the DB in /mnt/sdcard/
    // External folder DB access for Android 4+
       @Override
       public SQLiteDatabase openOrCreateDatabase(String name, int mode,CursorFactory factory, DatabaseErrorHandler errorHandler) {
             File externalFilesDir = Environment.getExternalStorageDirectory();
             if(externalFilesDir == null) 
                    return null;
             File dbFile = new File(externalFilesDir, SQLiteHelper.DATABASE_NAME);
             return super.openOrCreateDatabase(dbFile.getAbsolutePath(), Context.MODE_WORLD_WRITEABLE, null, errorHandler);
       }

       // External folder DB access for Android 2.3
    @Override
    public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory){
         File externalFilesDir = Environment.getExternalStorageDirectory();
         if(externalFilesDir == null) 
                return null;
         File dbFile = new File(externalFilesDir, SQLiteHelper.DATABASE_NAME);
         return SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), null, SQLiteDatabase.CREATE_IF_NECESSARY);
    }
于 2012-06-04T13:00:04.287 回答