1

I have searched a lot about this problem.My phone is not rooted. I want to copy a database file programmatically from my asset folder of the application to /system/usr of my Android device. So that i can access the database file from there and check whether my app's users were being able to upgrade the database. I know that i have to change the 'outFileName' and "outputStream" in the following code but i am not exactly sure how to specify the output path for /sytem/usr in the following code:

copyDBfromassetstodevicedrive.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                try {
                    // Open your local db as the input stream
                    InputStream myInput = myContext.getAssets().open("myDB.db");

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

            }
          });

thanks for any suggestions.

kzs
  • 1,111
  • 5
  • 20
  • 35

2 Answers2

1

This is not possible unless your device is rooted. However i don't see why you need to do this. Maybe if you can tell what you want to do, someone here can help you find a solution.

Anasthase
  • 491
  • 2
  • 7
  • even programmatically it's not possible? my app allows the user to add new rows to the sqlite database while running the app.I want to see whether the user was really able to add new rows. I guess it can be done using sql queries, for example: http://stackoverflow.com/questions/4017903/get-last-inserted-value-from-sqlite-database-android – kzs Dec 13 '11 at 16:32
  • You can copy from the assets folder to data/data/com.yourname/databases but not to system/usr. Would the former solve your problem? – silleknarf Dec 13 '11 at 16:52
  • It is not possible to copy the file where you want to put it (/system/usr). But you can of course copy it to either the application private (internal) storage, or the device external storage. See [this](http://developer.android.com/guide/topics/data/data-storage.html#filesInternal) for more information. – Anasthase Dec 13 '11 at 16:55
  • Hi, I have the same question: The purpose is my data base value is changing very frequently, I am getting the DB file from an external source, so that I want to put that file to the sd card and access the values from the SD card. In short I don't want the db file to be stored in to the data/data/pacagename/database, I want the default location of the database to the SD card. – DAS Feb 16 '12 at 12:34
0

private static String DB_PATH = Environment.getDataDirectory()+"/data/package-name/databases/";

private void copyDataBase() throws IOException
{


    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty d inb
    String outFileName = DB_PATH + DB_NAME;

    // Open the empty db as the output stream
    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();

    //Copy successful
    outFileName = DB_PATH + DB_SUCCESS;
    myOutput = new FileOutputStream(outFileName);
    myOutput.write(1);
    myOutput.flush();
    myOutput.close();
}
Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
Nikhil Pingle
  • 747
  • 7
  • 11