首页 > 解决方案 > java sqlite export import for Android10 以上

问题描述

下面的 java 代码在 Android 10 及更低版本中使用,由于限制,在 Android 10 及更高版本中不再可用。如何使以下代码适用于 android 10 以上版本。谢谢您的帮助。

 public static boolean importDb(Context context) {
        
    String str = "Notes";

    File file = new 

    File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), str);
    
     if (checkDbIsValid(file)) {
            return false;
     }
    
try {
    
@SuppressLint("WrongConstant") SQLiteDatabase openDatabase = 
      SQLiteDatabase.openDatabase(file.getPath(), null, 1);

      Cursor query = openDatabase.query(true, DATABASE_TABLE, null, null, null, null, null, null, null);

       NotesDbAdapter notesDbAdapter = new NotesDbAdapter(context);

       notesDbAdapter.open();

       query.moveToPosition(-1);

 while (query.moveToNext()) {

       notesDbAdapter.createNote(query.getString(1), query.getString(2), query.getString(3), 
      query.getString(4));

  }

            openDatabase.close();

            query.close();

            notesDbAdapter.close();

            return true;

     } catch (Exception e) {
            e.printStackTrace();
            return false;
     }
}

标签: javasqliteimportexport

解决方案


推荐阅读