0

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

Murillo Comino
  • 221
  • 3
  • 11
  • 1
    Have you tried adding `android:requestLegacyExternalStorage="true"` to the manifest [Manage scoped external storage access](https://developer.android.com/training/data-storage/files/external-scoped) – MikeT Sep 30 '19 at 22:57
  • I will try. Thanks – Murillo Comino Sep 30 '19 at 23:46

1 Answers1

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • My intention is to keep in a directory that will continue to exist after uninstalling the application. With getExternalFilesDir () after uninstalling the app present files will be removed – Murillo Comino Sep 30 '19 at 23:16
  • @MurilloComino just enable backups, then it will at least be restored when reinstalling (this should be disabled for debug builds). – Martin Zeitler Sep 30 '19 at 23:17
  • @MurilloComino: "My intention is to keep in a directory that will continue to exist after uninstalling the application" -- you will need to do that as part of a backup or export option. You no longer have direct filesystem access to locations like what you seek, and SQLite needs direct filesystem access. – CommonsWare Sep 30 '19 at 23:28
  • Thanks for the support, I will follow these suggestions. – Murillo Comino Sep 30 '19 at 23:47