Unable to create and copy file

My program should work like this:

1.Copy the new database in the program folder
2.Import the records from the old database to the new one.

      

But I am getting an exception for some reason. Why?

protected Object doInBackground(Object[] objects) {

        String LOCAL_DATABASE_PATH = getApplicationInfo().dataDir + File.separator +
                "databases" + File.separator;


        File fileDir = new File(LOCAL_DATABASE_PATH);
        if (!fileDir.exists())
            fileDir.mkdirs();

        File tempFile = new File(LOCAL_DATABASE_PATH + DATABASE_NAME);

        try {
            tempFile.createNewFile(); // here I catch exception

            InputStream is = SplashActivity.this.getAssets().open(
                    DATABASE_NAME);
            FileOutputStream os = new FileOutputStream(new File(
                    LOCAL_DATABASE_PATH, DATABASE_NAME));

            int bufferLength = 0;
            byte[] buffer = new byte[2048];

            while ((bufferLength = is.read(buffer)) > 0) {
                os.write(buffer, 0, bufferLength);
            }

            Preferences.getInstance(SplashActivity.this).
                    set(Preferences.IS_DATABASE_COPYING_ON_DEVICE, true);

            is.close();
            os.close();



        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

      

I am getting the following error

java.io.IOException: open failed: EACCES (Permission denied)
08-28 09:32:27.977  32558-32558/com.DriverNotes.AndroidMobileClientTest D/libEGLīš• loaded /system/lib/egl/libGLES_hawaii.so
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.errīš• at java.io.File.createNewFile(File.java:948)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.errīš• at com.DriverNotes.AndroidMobileClientTest.SplashActivity$DataBaseLoadTask.doInBackground(SplashActivity.java:73)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.errīš• at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.errīš• at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.errīš• at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.errīš• at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.errīš• at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.errīš• at java.lang.Thread.run(Thread.java:856)
08-28 09:32:27.987  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.errīš• Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
08-28 09:32:27.987  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.errīš• at libcore.io.Posix.open(Native Method)
08-28 09:32:27.987  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.errīš• at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
08-28 09:32:27.987  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.errīš• at java.io.File.createNewFile(File.java:941)
08-28 09:32:27.987  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.errīš• ... 7 more

      

+3


source to share


1 answer


When creating, tempFile

try using the fileDir

one you already created to make sure it exists.

File tempFile = new File(fileDir, DATABASE_NAME);

      

Before creating a new file, you must check if it exists by calling its method exits()

. And then instead of opening it again, you should keep using it. Or at least close it before opening it again.



if(!tempFile.exists())
   tempFile.createNewFile();

InputStream is = SplashActivity.this.getAssets().open(
                 DATABASE_NAME);
FileOutputStream os = new FileOutputStream(tempFile);

      

Create FileOutPutStream

with tempFile

or tempFile

to make sure your file is not locked.

+1


source







All Articles