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
606 views
in Technique[技术] by (71.8m points)

android - Access the phone internal storage to push in SQLite database file

I am developing my android application using Netbeans and java. When I am using the emulator I can access the File explorer and insert an SQLite database in to device internal memory by accessing the following path, data/data/com.example.helloandroid/database

But I can not access this location to push the SQLite File in to the phone's internal storage (location) when I am using the real device.

Can someone please help me how to add the file in to phones internal storage. Thanks

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I think the device doesn't have root permission, that's why you can't access it. If you want to do in your app with programmatically then it is possible. If anybody knows better then this please share it.

EDIT: ok, first of all,

1. copy your Database.db file in your projects assets folder.
2. now using code copy database file from /asset to device's internal storage 
   (data/data/<package name>/database folder).

For copy file use below code,

try {
     // Open your local db as the input stream
     InputStream myInput = myContext.getAssets().open("your database file name");

     // Path to the just created empty db
     String outFileName = "/data/data/<your_app_package_name>/databases/<database_file_name>";

     OutputStream myOutput = new FileOutputStream(outFileName);

     // transfer bytes from the inputfile to the outputfile
     byte[] buffer = new byte[1024];
     int length;
     while ((length = myInput.read(buffer)) > 0) 
         {
       myOutput.write(buffer, 0, length);
     }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
} 
catch (Exception e) 
{
     Log.e("error", e.toString());
}

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

...