DDMS文件浏览器无法访问数据\数据(HTC Desire HD的)

DDMS文件浏览器无法访问数据\数据(HTC Desire HD的)

问题描述:

我工作的一些SQLite的code,想检查数据库。如果我运行模拟器上的code,我能够从数据使用DDMS文件管理器\ DATA \ myProject的\数据库拉文件,但如果我在实际的硬件上运行它的数据\ data文件夹是不可访问。有没有解决这个其他任何方式比获取到手机root访问权限?

I'm working on some SQLite code and would like to examine the database. If I run the code on the emulator I am able to pull the file from data\data\myProject\databases using the DDMS file manager but if I run it on actual hardware the data\data folder is inaccessible. Is there any way around this other than gaining root access to the phone?

SQLite数据库实际上只是存储在手机内存中,您可以复制到手机的SD卡,然后轻松地从你的电脑访问它的文件。以下是这不正是你所需要的功能。只要确保使用功能前更改你的包名称和数据库名称。

SQLIte Database is actually just a file stored in phone memory, which you can copy to your phone SD card and then easily access it from your PC. Below is the function which does exactly what you need. Just make sure to change your package name and database name before using function.

public static void backupDatabase() throws IOException {
    //Open your local db as the input stream
    String inFileName = "/data/data/your.package.name/databases/database.sqlite";
    File dbFile = new File(inFileName);
    FileInputStream fis = new FileInputStream(dbFile);

    String outFileName = Environment.getExternalStorageDirectory()
                                                    + "/database.sqlite";
    //Open the empty db as the output stream
    OutputStream output = new FileOutputStream(outFileName);
    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = fis.read(buffer))>0){
        output.write(buffer, 0, length);
    }
    //Close the streams
    output.flush();
    output.close();
    fis.close();
}

正如你可以看到从code,数据库总是存储在文件夹中:

As you may observe from the code, database is always stored in the folder:

/data/data/your.package.name/databases/

名称(在我们的例子database.sqlite)是您扩展SQLiteOpenHelper时捡到一个。很明显,你还需要更换your.package.name与您的应用程序包的名称。

Name (in our example "database.sqlite") is the one you picked when extending SQLiteOpenHelper. Obviously, you also need to replace "your.package.name" with the name of your application package.