Access the phone internal storage to push in SQLite database file | by Ted James | May, 2024

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

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

Answered By — user370305

Answer Checked By — Marilyn (FixIt Volunteer)

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment

Scroll to Top