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.
Related
I'm getting NullpointerException while trying to access db in Assets folder. And the db is indicated red in color on Assets folder.
Image containing Db in Assets
public void createdatabase() throws IOException
{
System.out.println(" came into Create database ");
//Open your local db as the input stream
InputStream myinput = mycontext.getAssets().open(DB_NAME);
System.out.println(" myinput = "+myinput.toString());
System.out.println(" Got input ");
// Path to the just created empty db
String outfilename = DB_PATH + DB_NAME;
System.out.println(" outfilename = "+outfilename);
//Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream(outfilename);
// transfer byte to inputfile to outputfile
System.out.println(" created outputStream");
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();
}
Is there any way to check the db values in String?
DB_NAME does not match with the name of the file in assets folder.
Any reason why you did not tell the values?
I need a local database for my application.I created one and i encrypted it.(I know the pw).Now i want to load this db to my original application's assets folder.I want to decrypt it before copy.
I had a copy code like this this is working for unencrpyted db.
How can I translate this for my encrypted db.Thanks
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[5120];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
The proper way to do this would be to copy the encrypted database into a local folder. Then you can open it with SQLCipher and use sqlcipher_export() to convert it into a standard (non-encrypted) SQLite database.
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.
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();
}
Is it possible to supply prefilled SQLite DB to my app? I want to use db as ordinary SQLite db which will have tables filled manually and I want to include it into my .apk file.
Yes, include it in your assets folder and copy it into the /databases folder when your application first launches.
try this
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();
}
Including in the assets folder is one option (Apk size will be increased.)
How about storing the pre-filled db on any cloud storage service provider and downloading the file on the first run of the app?