我需要从我的应用程序读取另一个应用程序的 sqlite 数据库。我知道默认情况下这在 android 应用程序中是不可能的,但由于设备已获得 root 权限,我想我应该能够授予我的应用程序 root 访问权限并获取其他应用程序的数据库并读取它。
我不想通知设备用户并给我 SUPER_ACCESS
权限,所以这应该是一个匿名过程。另一个App已经实现了ContentProvider并且设置了exported = true
到目前为止,这是我尝试过的方法,但它仍然抛出无法打开数据库文件的异常。
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) {
...
}
有什么地方我做错了或者我应该采取不同的做法吗?
最佳答案
要从其他应用程序数据库读取数据,其他应用程序必须实现一个ContentProvider
并将该ContentProvider 定义为exported = true
有关内容提供商的更多信息,请查看 this .
然后,要访问 ContentProvider 数据,您应该实现 ContentResolver
而不是尝试直接访问其他数据库。
有关内容接收器的更多信息,请查看 this .
关于java - 如何在android中读取另一个应用程序数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44353488/