开发者

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

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);
    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜