How to restore dump file on android app - android

I've inserted a lot of data into my android application. Now I want to bundle this data into my app. I only have a database.db file now, how can I restore it to fresh installed app's database?

try this method:
private void importDatabaseFromAssets() {
try {
InputStream myInput = getAssets().open("your_database.db");
String DB_PATH = "/data/data/" + getPackageName() + "/databases/";
String outFileName = DB_PATH + "your_database_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();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

Related

How to change my storage from data/data to Android/data

am a rookie. The present location of my app folder when installed is in data/data but I want to change the folder location(com.time.notes) to Android/data which is where alot of the folder of the apps I install shows, this is the code am presently trying.
public void backup(String item) {
OutputStream myOutput = null;
InputStream myInput = null;
File directory;
String databasePath = "/data/data/"+getPackageName()+"/databases/Memo.db";
File f = new File(databasePath);
if (f.exists()) {
try {
directory = new File("/mnt/sdcard/"+getResources().getString(R.string.app_name)+"/Backup");
if (!directory.exists())
directory.mkdir();
if (item.equals("Backup")) {
myOutput = new FileOutputStream(directory.getAbsolutePath() + File.separator + "/"+getResources().getString(R.string.app_name));
myInput = new FileInputStream(databasePath);
Toast.makeText(this, "Backup Location " + directory, Toast.LENGTH_LONG).show();
} else if (item.equals("Restore")) {
myInput = new FileInputStream(directory.getAbsolutePath() + File.separator + "/"+getResources().getString(R.string.app_name));
myOutput = new FileOutputStream(databasePath);
Toast.makeText(this, "Backup Restored Successfully ", Toast.LENGTH_LONG).show();
}
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
} catch (Exception e) {
} finally {
try {
if (myOutput != null) {
myOutput.close();
myOutput = null;
}
if (myInput != null) {
myInput.close();
myInput = null;
}
} catch (Exception e) {
}
}
}
}

Opening a database from external storage Android

I was previously storing a sqlite database in my apps assets folder but have now moved the database to external storage.
My previous copyDatabase() method looked like this.
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
OutputStream myOutput = new FileOutputStream(DB_PATH);
byte[] buffer = new byte[AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT];
while (true) {
int length = myInput.read(buffer);
if (length > 0) {
myOutput.write(buffer, 0, length);
} else {
myOutput.flush();
myOutput.close();
myInput.close();
return;
}
}
}
The issue is I'm unsure how to create an InputStream for opening the database from external storage. I can't seem to find the external storage equivalent to myContext.getAssets().open(DATABASE_NAME);
The current database path:
DB_PATH = Environment.getExternalStorageDirectory().getPath().toString()+"/SoulInfoDatabase/BB2SoulDatabase.db";
Step 1: Give storage permission in your App Manifesto file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Step 2: Copy database to your custom SD card Path
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = context.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
new File(outFileName).createNewFile();
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();
}
Step 3: Then Open your database:
try {
db = SQLiteDatabase.openDatabase(DB_PATH + "/" + DB_NAME, null,
SQLiteDatabase.OPEN_READWRITE
| SQLiteDatabase.NO_LOCALIZED_COLLATORS);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
WHERE
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SoulInfoDatabase";
File file = new File(filePath);
if(!file.exists()){
file.mkdirs();
}
DB_PATH = filePath;
DB_NAME = "BB2SoulDatabase.sqlite";

How to copy another database apps to SDCard

I've install an application to my device. But i want to copy that database to SDcard to be used by my apps. I used this code to copy database. But it's failed. And need "su" command
public void copydatabase() throws IOException{
String [] cmd1 = { "su", "cp", "/data/data/com.apps/databases/data01.db", "/mnt/extSdCard/data01.db"};
Process process = new ProcessBuilder(cmd1).start();
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Any some code to copy that to SDCard without "su" command??
public static void copyDataBase(Context mActivity) throws IOException {
InputStream myInput = new FileInputStream(
new File("/data/data/" + mActivity.getPackageName()
+ "/databases/" + "xyz.sqlite"));
File files = new File("/sdcard/files/");
files.mkdirs();
String outFileName = "/sdcard/files/xyz.sqlite";
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int bufferLength;
while ((bufferLength = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, bufferLength);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
I hope its useful to you...

Android - Copying Database from Assets

Here is the code I am using (found in many answers):
InputStream myInput;
try {
myInput = iNezamApplication.getAppContext().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();
} catch (IOException e1) {
e1.printStackTrace();
}
However, I always get an exception after reaching the OutpoutStream line:
java.io.FileNotFoundException: /data/data/package_name/databases/databasename.db: open failed: ENOENT (No such file or directory)
I tried something like this..
final String[] sample_dbName = {"DrugsNew.db"};
int assetDbSize = sample_dbName.length;
File databaseFile = new File( "/data/data/com.handyrep2.ui/databases");
// check if databases folder exists, if not create one and its subfolders
if (!databaseFile.exists()){
databaseFile.mkdir();
}
for(int i=0;i<assetDbSize;i++){
String outFilename =null;
outFilename = "/data/data/com.handyrep2.ui/databases"+ "/" + sample_dbName[i];
File sampleFile = new File(outFilename);
try {
InputStream in = activity.getAssets().open("offlinedb/"+sample_dbName[i]);
OutputStream out = new FileOutputStream(outFilename);
// Transfer bytes from the sample input file to the sample output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.flush();
// Close the streams
out.close();
in.close();
}catch(IOException e) {
}
}
Is "package_name" a substitute for your real package name just to post it here or do really use this in your DB_PATH? :)
you must create a File object at first

in android data/data/.....geting only half database

i have created database for android application and its size is 30 kb i have placed this database in to asset folder and used below code for paste it in to data/data/........
i am getting only 1/3 of my database after run on emulator. Is any one faced this problem earlier.
//enter code here
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does't exist yet.
Log.i("database", "NOT EXIST");
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase()
{
FileInputStream myInput = null;
String outFileName;
FileOutputStream myOutput = null;
try{
// Open your local db as the input stream
//InputStream myInput = myContext.getResources().openRawResource(R.drawable.menu);
myInput = (FileInputStream) myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
try{
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
}catch (Exception e) {
Log.i("catch ", "Ecxeption"+e.getMessage());
// TODO: handle exception
}
}catch (Exception e) {
// TODO: handle exception
}finally{
try{
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
catch (Exception e2) {
// TODO: handle exception
}
}
}
when i am running this code in emulator then i found my database in to data/data/.......
but i found only 7 kb database.
please help me.
How about changing
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
to
while ((length = myInput.read(buffer)) != -1) {
myOutput.write(buffer, 0, length);
}
I don't think the stream is done until it hits -1

Categories

Resources