FAQ

[android-ndk] Error copying database from assets directory to application package 'databases' directory

Abhi
Oct 7, 2011 at 4:02 pm
Hi,

I have a Native C/C++ code that makes calls to sqlite3 database. I
have been able to successfully compile the code and generate the .so
library. The database I am using is in the assets directory of the
application and I want to copy it to the application package's
"databases" directory for the native code to query it from the
location '/data/data/*package name*/databases/*db name*.

I am using the following code to copy the database over

InputStream myInput = this.getAssets().open("ereview.db");

String outFileName = DB_PATH + "ereview.db";

//Open the empty db as the output stream
try{

OutputStream myOutput = null;

myOutput = new FileOutputStream(outFileName);

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

//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
catch (FileNotFoundException ef){
Log.e(TAG, "This is the problem");
}

I get the FileNotFoundException and having traced down the actual
reason, I see that up to the point where I am creating a new
FileOutputStream for the given path, everything seems to be working
fine. Its only while executing "myOutput = new
FileOutputStream(outFileName);" that I am getting that error, I think?

I am using the same way of copying the database to the databases
location in another application and it works fine there. I am
confused.

Any help highly appreciated.

Thanks,
AB

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to android-ndk@googlegroups.com.
To unsubscribe from this group, send email to android-ndk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
reply

Search Discussions

2 responses

  • Dianne Hackborn at Oct 7, 2011 at 8:45 pm
    Application code should not directly touch the databases directory, that is
    owned by the framework for when you use the associated APIs dealing with
    databases.

    Also you should never hard-code a path like that. Context has APIs for
    getting the paths to various things in your data directory.
    On Fri, Oct 7, 2011 at 4:01 PM, Abhi wrote:

    Hi,

    I have a Native C/C++ code that makes calls to sqlite3 database. I
    have been able to successfully compile the code and generate the .so
    library. The database I am using is in the assets directory of the
    application and I want to copy it to the application package's
    "databases" directory for the native code to query it from the
    location '/data/data/*package name*/databases/*db name*.

    I am using the following code to copy the database over

    InputStream myInput = this.getAssets().open("ereview.db");

    String outFileName = DB_PATH + "ereview.db";

    //Open the empty db as the output stream
    try{

    OutputStream myOutput = null;

    myOutput = new FileOutputStream(outFileName);

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

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
    }
    catch (FileNotFoundException ef){
    Log.e(TAG, "This is the problem");
    }

    I get the FileNotFoundException and having traced down the actual
    reason, I see that up to the point where I am creating a new
    FileOutputStream for the given path, everything seems to be working
    fine. Its only while executing "myOutput = new
    FileOutputStream(outFileName);" that I am getting that error, I think?

    I am using the same way of copying the database to the databases
    location in another application and it works fine there. I am
    confused.

    Any help highly appreciated.

    Thanks,
    AB

    --
    You received this message because you are subscribed to the Google Groups
    "android-ndk" group.
    To post to this group, send email to android-ndk@googlegroups.com.
    To unsubscribe from this group, send email to
    android-ndk+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/android-ndk?hl=en.

    --
    Dianne Hackborn
    Android framework engineer
    hackbod@android.com

    Note: please don't send private questions to me, as I don't have time to
    provide private support, and so won't reply to such e-mails. All such
    questions should be posted on public forums, where I and others can see and
    answer them.

    --
    You received this message because you are subscribed to the Google Groups "android-ndk" group.
    To post to this group, send email to android-ndk@googlegroups.com.
    To unsubscribe from this group, send email to android-ndk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
  • Krystian Bigaj at Oct 10, 2011 at 5:15 pm
    In case if you would targeting 2.3+ and using SQLite datebase as
    readonly you can use http://code.google.com/p/sqlite-ndk/
    It allows you to use access SQLite databases directly from 'assets'
    directory (without need to copy to SD card).
    On Oct 7, 6:01 pm, Abhi wrote:
    ... The database I am using is in the assets directory of the
    application and ...
    --
    You received this message because you are subscribed to the Google Groups "android-ndk" group.
    To post to this group, send email to android-ndk@googlegroups.com.
    To unsubscribe from this group, send email to android-ndk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post