简体   繁体   中英

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

How can i do the following situation for android Q (Api 29)?

My intention is to keep in a directory that will continue to exist after uninstalling the application.

It has the following class with its constructor:

    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

    }

And in my DAO when calling the database:

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

}

Using SQLiteOpenHelper is there any way to create database in Public Documents folder for Api 29?

Thank you for your attention

Putting a live SQLite database on external storage is not wise for any version of Android. There is a greater risk of data corruption, as other apps might attempt to work with that file.

However, if you wish to ignore that advice, you can use getExternalFilesDir() on Android 10 as a base directory for your database. This is accessible by the user, albeit in a not-too-friendly location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM