John Does
John Does

Reputation: 13

How to read another application database in android

I need to read sqlite database of another application from my application. I know that by default this is not possible within android applications, but as the device is rooted I suppose that I should be able to give my application root access and grab the other application's database and read it.

I don't want the device user to be notified and give me SUPER_ACCESS permission, so this should be an anonymous process. The other App has already implemented ContentProvider and has set exported = true

This is what I have tried so far but it still throws an exception that it could not open the database file.

try {

    File dbFile = new File("/data/data/com.sam.sample/databases/sample.db");

    if(dbFile.exists()) {

        String [] cmd = {"su", "-c", "chmod", "777", file.getPath()};

        Process process = new ProcessBuilder(cmd).start();
        process.waitFor();

        SQLiteDatabase db = SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);

        Cursor cursor = db.rawQuery("SELECT * FROM sample", null);

        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                    Log.v("FROM DATABASE", name);
                    cursor.moveToNext();
                }
            }
        }

        cursor.close();
        db.close();

    }
}
catch(IOException e) {
   ...
}

Is there anything that I am doing wrong here or that I should do differently?

Upvotes: 1

Views: 2749

Answers (1)

AlexTa
AlexTa

Reputation: 5251

To read data from other application database, it is necessary that the other application had implemented a ContentProvider and had defined that ContentProvider as exported = true

For more info about content providers, check out this.

Then, to access ContentProvider data you should implement a ContentResolver instead of try to access other database directly.

For more info about content receivers, check out this.

Upvotes: 2

Related Questions