I am using this code and it keeps only getting to the output file line and throws the exception then. Can anyone see what the issue might be with this line?
try{
Log.e("Trying","try");
// Local database
InputStream input = new FileInputStream("/data/data/package/databases/database");
Log.e("Input","in");
// create directory for backup
// Path to the external backup
OutputStream output = new FileOutputStream("/sdcard/android/package/databases/mydatabase.db");
Log.e("Output","out");
// transfer bytes from the Input File to the Output File
byte[] buffer = new byte[1024];
Log.e("Buffer","Buff");
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
Log.e("After While","try");
output.flush();
output.close();
input.close();
} catch (IOException e) {
throw new Error("Copying Failed");
}
I doubt this is the cause of your problem, but the following two lines:
should be turned into:
This is because the sdcard and data directories may be in different places on different phones. And often, you need to do
/mnt/sdcard/to actually reference the sd card, but the best way is still to use Files and useEnvironmentas I showed above.You would of course need to put it all into a try block as you already have, and then within the catch, you will need to put
e.printStackTrace();and then if an error is thrown, you can look at the errors from the logcat to determine where your code fails instead of using a log every couple of lines.And, in your Manifest you have to have the following permission: