开发者

Access the phone internal storage to push in SQLite database file

开发者 https://www.devze.com 2023-04-01 22:02 出处:网络
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

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开发者_如何学C, 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


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

精彩评论

暂无评论...
验证码 换一张
取 消