1

I am using this below code to copy my database to internal storage but it is not helping me out and always getting this error. I have gone through lots of questions and answers and tried lots of blog posts but am unable to make it work. My phone is HTC One X

I have also added one permission in manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />



public static boolean backUpDataBase(Context context)
    {
        String packageName = "com.projectWork.mobile";
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (Environment.MEDIA_MOUNTED.equals(sd))
            {
                String currentDBPath = "//data//"+ packageName +"//databases//"+"appDb.db";
                String backupDBPath = "appDb.db";
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
                Toast.makeText(context, backupDB.toString(), Toast.LENGTH_LONG).show();

            }
        } catch (Exception e) {

            Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();


        }

        return true;
    }

This is the error I am getting

java.io.FileNotFoundException: /mnt/sdcard/appDb.db: open failed: EROFS (Read-only file system)

Please guide as I want to store all my errors in the database so that we can Analyse them later.

Thanks

Shax
  • 4,207
  • 10
  • 46
  • 62
  • why the double '//' ? the error comes from your sdcard being mounted read only, it would seem. I'm not sure about that FileChannel thing. maybe plain fileinput/outputstream would work better ? – njzk2 Oct 22 '12 at 12:02
  • you are right, the problem comes when I try to write the file in the sd card then it says readonly error. ...please suggest. My mobile is not rooted and I don't want to do that either – Shax Oct 22 '12 at 19:45
  • 2
    start by trying to create a file a write to it simply on the sdcard. it may be something stupid like the sdcard is locked. – njzk2 Oct 23 '12 at 07:27

1 Answers1

1

Try this way. It work in my side.

String currentDBPath = "\\data\\"+ packageName +"\\databases\\"+"appDb.db";

Also Follow this post

Thanks.

Community
  • 1
  • 1
Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37