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

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

Related

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 restore dump file on android app

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

How to copy database file from asset folder to /data/data/<package-name>/database folder

I have put a database file in to assets folder and want to copy it to /data/data//database folder . For this purpose , I have written the following code :
public void copy()
{
try
{
String dest_path = "/data/data/"+getPackageName()+"/database/sultandatabase";
File f = new File(dest_path);
if( !f.exists() )
{
copy_database(getBaseContext().getAssets().open(db_name),new FileOutputStream(dest_path));
}
}
catch(Exception e)
{
Toast.makeText(this,"Sorry the file can not be opended", Toast.LENGTH_LONG).show();
}
}
public void copy_database(InputStream io ,OutputStream ou) throws Exception
{
byte[] buffer = new byte[1024] ;
int lenght;
while( ( lenght = io.read(buffer)) > 0 )
ou.write(buffer);
io.close();
ou.close();
}
When I call copy() the database file is not copied . What is the possible reason ?? How can I fix this ?? Instead a toast is shown up . That means my program gets Exception ?? But why ??
i have this code for copy:
private void copyDataBase()
{
try
{
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
String outFileName = DATABASE_PATH + 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(Exception ex)
{
ex.printStackTrace();
}
}

Copy Database from assets folder in unrooted device

I am trying to copy DB from assets folder to device. This code is working fine on Emulator and rooted Device. I just want to know is it create any problem on unrooted device or it will work same.
private void StoreDatabase() {
File DbFile = new File(
"data/data/packagename/DBname.sqlite");
if (DbFile.exists()) {
System.out.println("file already exist ,No need to Create");
} else {
try {
DbFile.createNewFile();
System.out.println("File Created successfully");
InputStream is = this.getAssets().open("DBname.sqlite");
FileOutputStream fos = new FileOutputStream(DbFile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
System.out.println("File succesfully placed on sdcard");
// Close the streams
fos.flush();
fos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
This will work for sure in all devices and emulator, no need to root.
/**
* Copies your database from your local assets-folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transfering bytestream.
* */
private void copyDataBase(String dbname) throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(dbname);
// Path to the just created empty db
File outFileName = myContext.getDatabasePath(dbname);
// 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 database file from assets folder inside the apk to the system database path.
* #param context Context
* #param databaseName Database file name inside assets folder
* #param overwrite True to rewrite on the database if exists
* #return True if the database have copied successfully or if the database already exists without overwrite, false otherwise.
*/
private boolean copyDatabaseFromAssets(Context context, String databaseName , boolean overwrite) {
File outputFile = context.getDatabasePath(databaseName);
if (outputFile.exists() && !overwrite) {
return true;
}
outputFile = context.getDatabasePath(databaseName + ".temp");
outputFile.getParentFile().mkdirs();
try {
InputStream inputStream = context.getAssets().open(databaseName);
OutputStream outputStream = new FileOutputStream(outputFile);
// transfer bytes from the input stream into the output stream
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
// Close the streams
outputStream.flush();
outputStream.close();
inputStream.close();
outputFile.renameTo(context.getDatabasePath(databaseName));
} catch (IOException e) {
if (outputFile.exists()) {
outputFile.delete();
}
return false;
}
return true;
}
I am not sure, but this works on every device I have tested on. I stole this method (from somewhere here) and made it generic for both backing up and restoring:
public static void movedb(File srcdb, File destdb)
{
try
{
if (Environment.getExternalStorageDirectory().canWrite())
{
if (srcdb.exists())
{
FileChannel src = new FileInputStream(srcdb).getChannel();
FileChannel dst = new FileOutputStream(destdb).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
else
{
//ERROR: "Database file references are incorrect"
}
}
else
{
//ERROR: "Cannot write to file"
}
}
catch (Exception e)
{
//ERROR: e.getMessage()
}
}
Then I just back it up by calling:
movedb(this, getDatabasePath(getDbName()), new File(Environment.getExternalStorageDirectory(), getDatabaseBackupPath()));
Where getDatabasePath() and getDatabaseBackupPath() are just string values
private void copyDataBase(Context context) throws IOException {
//Log.i(TAG, "Opening Asset...");
// Open your local db as the input stream
InputStream myInput = context.getAssets().open(DBHelper.DATABASE_NAME);
// Log.i(TAG, "Getting db path...");
// Path to the just created empty db
File dbFile = getDatabasePath(DBHelper.DATABASE_NAME);
if (!dbFile.exists()) {
SQLiteDatabase checkDB = context.openOrCreateDatabase(DBHelper.DATABASE_NAME, context.MODE_PRIVATE, null);
if (checkDB != null) {
checkDB.close();
}
}
//Log.i(TAG, "Getting output stream...");
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(dbFile);
// Log.i(TAG, "Writing data...");
// 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();
}
This works for Kotlin.
assets.open("sqlite_db_in_assets.db")
.copyTo(getDatabasePath("sqlite_db_in_device.db").outputStream())
While technically feasible, I don't believe copying (either to or from) potentially live database file is a good idea.

Copying database form assets fails on emulator

I used this tutorial to include a database file to my android app. It works fine on my HTC Decire HD. I wanted to run it on emulator to see if tablet layouts look well. Unfortunately the app fails with an error.
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){ <------ HERE, at first iteration
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
The message for this error is just 'null', nothing more. Can this be fixed?
private void copyfromAsset()
{
try {
String FILE_TO_READ="data.txt"; //file in asset folder
String TEMP_FILE_NAME="temp.txt"; //or whatever file name you want to give
byte[] buffer = new byte[1024];
int len1 = 0;
InputStream istr=(con.getAssets().open(FILE_TO_READ));
FileOutputStream fos=openFileOutput(TEMP_FILE_NAME,MODE_WORLD_READABLE);
while ((len1 = istr.read(buffer)) !=-1) {
fos.write(buffer, 0, len1); // Write In FileOutputStream.
}
fos.flush();
fos.close();
istr.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
try this method it is working fine for me....hit accept if you found usefull..
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
}
else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are going to be able to overwrite that
// database with our database.
try {
copyDataBase();
}
catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* #return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
/**
* Copies your database from your local assets-folder to the just created
* empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transferring byte-stream.
*/
private void copyDataBase() throws IOException {
// Open your local DB as the input stream
InputStream myInput = mContext.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 input-file to the output-file
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();
}

Categories

Resources