簡體   English   中英

Api 29 (Android Q) 的外部應用程序目錄中的 SQLiteOpenHelper

[英]SQLiteOpenHelper in an external application directory for Api 29 (Android Q)

對於 android Q (Api 29),我該如何做以下情況?

我的意圖是保留在卸載應用程序后將繼續存在的目錄中。

它具有以下 class 及其構造函數:

    public class SqlHelper extends SQLiteOpenHelper {
    //constructor
     SqlHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

    //rest of the class

    }

在我的 DAO 中調用數據庫時:

public class Dao {

    private Context context;
    protected SQLiteDatabase db;

    public Dao(Context context) {
        this.context = context;
    }

    protected void openDataBase() {
        String pathDB = FOLDER_NAME_DBASE;
        //getExternalStorageDirectory() deprecated Api 29
        File dir = new File(Environment.getExternalStorageDirectory(), pathDB);


        String dBasePath = dir.getPath() + "/" + mydatabse.db3;

        SqlHelper varHelp = new SqlHelper(
                context,
                dBasePath,
                null,
                DBASE_VERSION
        );

        db = varHelp.getWritableDatabase();
    }

   //rest of the class

}

使用 SQLiteOpenHelper 有沒有辦法在 Api 29 的公共文檔文件夾中創建數據庫?

感謝您的關注

對於任何版本的 Android 而言,將實時 SQLite 數據庫放在外部存儲上都是不明智的。 數據損壞的風險更大,因為其他應用程序可能會嘗試使用該文件。

但是,如果您希望忽略該建議,您可以使用getExternalFilesDir()作為數據庫的基本目錄。 用戶可以訪問它,盡管位置不太友好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM