3

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

JibW
  • 4,538
  • 17
  • 66
  • 101
  • cant you keep the database file in assets folder and copy to document folder programmatically? – jithinroy Sep 01 '11 at 10:22
  • I can simply push in the database file when using the emulator, With the DDMS -> File explorer. I need To do the same way when using the real device. – JibW Sep 01 '11 at 10:39

1 Answers1

10

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());
}
user370305
  • 108,599
  • 23
  • 164
  • 151
  • can you please explain, how to move a sqlite database file in to phones internal memory programatically. – JibW Sep 01 '11 at 10:52
  • for what reason you want to do that? for your application? – user370305 Sep 01 '11 at 10:54
  • I have created a SQLite database with many records.(Reading some xml files). So this complete application works with the SQLite database records. I need to test it with the real device and stucked..!!!! – JibW Sep 01 '11 at 10:59
  • Its all ok, But please clarify you are working on your application and you are using this database. and now your are testing it on real device, M I right? – user370305 Sep 01 '11 at 11:01
  • It Works when using the emulator. Can go to DDMS-> File Explorer and push in. But when use real devide not – JibW Sep 01 '11 at 11:03
  • please please, explain in brief, I m thinking you are working on one app with eclipse ide and you have a ready sqlite database which one you are using in your app by putting it in file/file/package name/database folder? right? – user370305 Sep 01 '11 at 11:06
  • Yes, exactly. That was i was doing. But when I need to push in the database file into real device problem occured. What u think is correct – JibW Sep 01 '11 at 11:44
  • Yes user370305. That was exactly I wanted. Thank you soooo much. Thanks again for your time and the help. really appreciate. – JibW Sep 01 '11 at 11:58
  • It works on most devices but for some device it doesnt .Have got any idea about this.? – Abx Jul 01 '13 at 13:01
  • @Abhilash - What types of problem are you facing? – user370305 Jul 01 '13 at 14:01
  • 1
    @Abhilash - Instead of hard coded path `/data/data/` use `public abstract File getDatabasePath (String name)` method. – user370305 Jul 01 '13 at 15:49
  • I got it now,I used 'File dirFile = this.getDir("dummydb", Context.MODE_PRIVATE);' and 'String outFileName = dirFile.getAbsolutePath()+ "/" + dbname;' – Abx Jul 02 '13 at 04:10