how to access path of database or file of different app in android

最后都变了- 提交于 2020-01-21 14:20:29

问题


Is it possible to get path of database file in android at path : "/data/system/accounts.db"

in my app i want to use this database, but not getting its path. if i do hardcoding and remove the file i'm able to do it. But i want to access it as database so that i can drop the table.

Any help would be appreciable.

code i tried:

    private SQLiteDatabase db;
    File dbFile = mContext.getDatabasePath("accounts.db");
    if (dbFile.exists())
    {
        db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
        db.execSQL("DROP TABLE IF EXISTS accounts");
        db.setVersion(db.getVersion()+1);
    }

回答1:


You can use android.os.Environment.getDataDirectory() to get the path to the data directory. Don't use the mContext.getDatabasePath if the db is not in the data dir of your app.

Also see Is it possible to move the internal DB to the SDCard?

if you want to search, do a recursive file search via something like this:

private void searchFile(String startingPoint) {

    Files[] files = File("/data").listFiles(); 
    for (File f:files) {
        if (f.isDirector) searchFile(f);
        else if (f.getName().equals("accounts.db")) {
            // delete db here...
            // and exit the search here...
        }
    } 
}

and check if it contains the accounds.db. If you reach another subdirectory, call your own function recursively with the subfolder as the starting point, and so on....

But make sure you have read access to the relevant folder you set as starting point to start the search.



来源:https://stackoverflow.com/questions/3474538/how-to-access-path-of-database-or-file-of-different-app-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!