Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can`t create and copy the file

My program should work as follows:

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

But I for some reason get an exception. 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
like image 507
Artem Avatar asked Nov 24 '25 06:11

Artem


1 Answers

When creating the tempFile try using the fileDir you already created to get sure it exists.

File tempFile = new File(fileDir, DATABASE_NAME);

Before you create a new file you should check if it already exists by calling its exits() method. And then instead of opening it again you should actually keep on using it. Or at least close it before opening again.

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

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

Create you FileOutPutStream using the tempFile or close tempFile to get sure your file is not locked.

like image 52
John Doe Avatar answered Nov 25 '25 19:11

John Doe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!