0

I'm using the code below to try and move my database file to my sdcard. I have no problems except that I get a redline under sd. Any ideas?

File data = Environment.getDataDirectory();
if (sd.canWrite()) {
    String currentDBPath = "\\data\\application.package\\databases\\name";
    String backupDBPath = "name";
    File currentDB = new File(data, currentDBPath);
    File backupDB = new File(sd, backupDBPath);
    if (currentDB.exists()) {
        FileChannel src;
    try {
    src = new FileInputStream(currentDB).getChannel();
        FileChannel dst = new FileOutputStream(backupDB).getChannel();
        try {
        dst.transferFrom(src, 0, src.size());
    src.close();
            dst.close();
        } catch (IOException e) {                                                    
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }

enter image description here

enter image description here

EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • Are you sure in correctness of slashes? – teoREtik Nov 13 '12 at 13:18
  • I'm not sure of the correctness of the slashes. I'm just trying to email myself a copy of the database following this old question. http://stackoverflow.com/questions/5906703/how-to-attach-database-file-to-an-email-in-android – EGHDK Nov 13 '12 at 13:19
  • Updated the question with a picture of the errors. – EGHDK Nov 13 '12 at 13:22
  • 2
    free tip : usually, with an error, there is a message. its goal is not to entertain you by looking pretty, but rather to give you context, descrption and information regarding the error. Free tip 2 : read the message. – njzk2 Nov 13 '12 at 13:34

2 Answers2

1

You can only use a variable if you create an instance of it:

Put this before your code:

File sd = Environment.getExternalStorageDirectory();
Frank
  • 16,476
  • 7
  • 38
  • 51
0

if you are using SQLite database try this:

public class _DBHelper extends SQLiteOpenHelper {

    public boolean backUp() throws Exception
        {
            InputStream input = null;
            OutputStream output = null;

            try {
                SQLiteDatabase db = this.getReadableDatabase();
                String strSource = db.getPath();

                String strDest = Utilities.getAppDocumentsFolder(_context) + "/"
                        + DATABASE_NAME;

                File fileDest = new File(strDest);
                if (fileDest.exists())
                {
                    fileDest.delete();
                }

                input = new FileInputStream(strSource);

                output = new FileOutputStream(strDest);

                byte[] buffer = new byte[1024];
                int length;
                while ((length = input.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }
            } catch (Exception e) {
                throw e;
            } finally
            {
                if (output != null)
                {
                    output.flush();
                    output.close();
                }
                if (input != null)
                {
                    input.close();
                }
            }

            return true;
        }
}
Bob
  • 22,810
  • 38
  • 143
  • 225
  • I like that piece of code, but I am having some problems with it. I have updated my question. – EGHDK Nov 13 '12 at 13:35