-1

I found a sweetie function to copy my Database File from the original path to External Storage Card:

public static void copyFile(File src, File dst) throws IOException {
        if(!dst.exists()) {
            dst.createNewFile();
        }
        else{
            dst.delete();
            dst.createNewFile();
        }
        InputStream in = new FileInputStream(src);
        try {
            OutputStream out = new FileOutputStream(dst);
            try {
                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }

But I'm not able to call that function, here my code on a simple button:

exportDatabase.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(Info.this, getDatabasePath(Info.this, "Library.sqlite"), Toast.LENGTH_SHORT).show();
        File srcFile = new File(getDatabasePath(Info.this, "Library.sqlite"));
        File dstFile = new File((getExternalStorageDirectory() + "/Library.sqlite"));
        copyFile(srcFile, dstFile);
    }
});

This line of code copyFile(srcFile, dstFile); is marked as error. On mouse over I receive an Unhandled Exeption:java.IO.IOExeption

How can I call it correctly?

Edit: To complete missing code, here the functions used in the button:

public String getDatabasePath(Context context,String databaseName)
{
    return context.getDatabasePath(databaseName).getAbsolutePath();
}
public String getExternalStorageDirectory() {
    String externalStorageDirectory;
    externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
    return externalStorageDirectory;
}

I just tested my solution in Memu (android emulator) and that code rocks! I'm now able to Backup and Restore my SQLite database. (to restore simple invert dst and src file paths). Hope this will help. Thanks to all.

3 Answers3

2

This line of code copyFile(srcFile, dstFile); is marked as error. On mouse over I receive an Unhandled Exeption:java.IO.IOExeption

Yes, Its behaving as intended. because Your copy() method may throws java.IO.IOExeption

Read here about What throws an IOException

You need to sound it by try-catch to handle Exception

Try this

try {
     copy(srcFile, dstFile);
 } catch (IOException e) {
     e.printStackTrace();
 }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

You are not handling the possible Exception. Wrap it around in a try-catch e.g.

try {
    copyFile(srcFile, dstFile);
} catch (IOException e) {
    // handle it
}
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
1

Since you said that it throws IOException, you should catch the exception using a catch . ie wrap your code inside a tr-catch block as below

try{
    copyFile(srcFile, dstFile);
}
catch(IOException e){
    e.printStackTrace();
}
Lal
  • 14,726
  • 4
  • 45
  • 70