copy a database file programatically from asset folder of the application - android

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.

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.

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

Related

Android SQLite database re-Import

in my project i have imported a sqlite database using this..
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
It Works fine. But When I delete the database file from the DDMS File Explorer and try to import it again, the code breaks down.
it says no such file found and create a empty database...
Is there any way to overcome this problem?
You Have to delete the previous database before import it again.
use this Context.deleteDatabase("DB_NAME")

android database copy error through programming

I have a strange problem I am trying to copy database from asset folder to app database folder "/data/data/YOUR_PACKAGE/databases/" by the below code. this is working fine for Samsung mobile not working for gionee mobile I don't know why please help
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 db
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();
}
How do you generate the DB_PATH? Maybe it is not available on the gionee mobile device (because the manufacturer modded the OS).
You should use the code below to determine outFileName:
String outFileName = context.getDatabasePath(DB_NAME).getPath();
If that still throws an exception, please update your post with more details.

Copy two SQLite Database in asset folder in Android

For the last few days I've been trying to copy two databases in the asset folder, but could not get any success.
One database I managed to copy and access it. But the second one, I need your help.
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 db
String outfilename = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream("/data/data/(packagename)/databases /(datbasename).sqlite");
// transfer byte to inputfile to 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();
}
Do this for your second database.

No such table on HTC desire HD (SQLite database on Android)

Please take a look at the following function :
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 db
String outFileName = DB_PATH + DB_NAME;
File f = new File(DB_PATH);
if (!f.exists()) {
f.mkdir();
}
//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;
//myInput.read(buffer);
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
Where DB_PATH is "/data/data/application package/databases/". Is it possible to set DB_PATH="" ? Actually setting DB_PATH = "/data/data/application package/databases/". works fine on most devices, but does not work on HTC desire HD ( discussed here , here , here and here ). This device does not find the mentioned path on the device.
Thanks
You should set your DB_PATH like this:
String DB_PATH = context.getDatabasePath(DB_NAME).getAbsolutePath()
That will give you the full path to your Database.. then you can use that as your outFileName.

Android - How to save file from server socket to sdcard pdf and jpg...?

I made server on my PC and Client on android...I write jpg file through socket and client android read and display successfully. But i want to save that to sdcard. Please help...
private void copytoSD() throws IOException{
//Open your local file as the input stream
InputStream myInput = //your input Stream
// Path to the just created empty db
String outFileName = Environment.getExternalStorageDirectory().toString+"/filename.jpg";
//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[2048];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}

Categories

Resources