Android, how to get databases directory on a nonrooted phone?

695 views Asked by At

I am trying to get a list of the databases that my Android SQLite program is working with. If I understand correctly, the databases are in:

data/data/[packagename]/databases

the databases are created using:

SQLiteDatabase.execSQL(DATABASE_CREATE);

And then I try to get a list using:

String[] instrumentFileList = MainActivity.this.fileList();

However, this returns a null array. I know the SQL database is being created because other parts of my app are able to access it. Is this failure to get a list because my phone is not rooted?

Also, is there a way to get a list of files in that directory using adb?

Any help is greatly appreciated.

1

There are 1 answers

0
alijandro On

How is the implementation of your method fileList()? Every database is stored in a separated file under /data/data/[packagename]/databases, usually the database name will suffix with ".db", you could achieve this by just list this directory.

    if (getDatabasePath(DBNAME) != null) {
        for (File f : getDatabasePath(DBNAME).getParentFile().listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                if (pathname.getName().matches("^.*\\.db$")) {
                    return true;
                }
                return false;
            }
        })) {
            Log.d(TAG, f.getAbsolutePath());
        }
    }

If you want to list the database file from adb, you can do like that

adb shell ls /data/data/[youpackagename]/databases/*.db

In some situation, it might not work because of permission restriction, however you can set the database directory permission to world readable in you application.