Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

android - DDMS file explorer can't access datadata (HTC Desire HD)

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 datadatamyProjectdatabases using the DDMS file manager but if I run it on actual hardware the datadata folder is inaccessible. Is there any way around this other than gaining root access to the phone?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...