1

I am trying to create a database through my android application, which is working on an emulator.

However, as I cant view the database directly on my Samsung device (it is not rooted) I am trying to transfer the database file to the SD card.

The file is not successfully transferring to the SD card, when I try and open it in eclipse it is not there.

I am getting the following error in Logcat:

07-15 14:31:07.035: E/mypck(17369): /data/com.example.multapply/databases/MultapplyDatabase.db: open failed: ENOENT (No such file or directory)
07-15 14:31:07.035: E/mypck(17369): java.io.FileNotFoundException: /data/com.example.multapply/databases/MultapplyDatabase.db: open failed: ENOENT (No such file or directory)
07-15 14:31:07.035: E/mypck(17369):     at libcore.io.IoBridge.open(IoBridge.java:409)
07-15 14:31:07.035: E/mypck(17369):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
07-15 14:31:07.035: E/mypck(17369):     at com.example.multapply.ExportDatabaseFileTask.copyFile(ExportDatabaseFileTask.java:71)
07-15 14:31:07.035: E/mypck(17369):     at com.example.multapply.ExportDatabaseFileTask.doInBackground(ExportDatabaseFileTask.java:49)
07-15 14:31:07.035: E/mypck(17369):     at com.example.multapply.ExportDatabaseFileTask.doInBackground(ExportDatabaseFileTask.java:1)
07-15 14:31:07.035: E/mypck(17369):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-15 14:31:07.035: E/mypck(17369):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-15 14:31:07.035: E/mypck(17369):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-15 14:31:07.035: E/mypck(17369):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-15 14:31:07.035: E/mypck(17369):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-15 14:31:07.035: E/mypck(17369):     at java.lang.Thread.run(Thread.java:841)
07-15 14:31:07.035: E/mypck(17369): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-15 14:31:07.035: E/mypck(17369):     at libcore.io.Posix.open(Native Method)
07-15 14:31:07.035: E/mypck(17369):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-15 14:31:07.035: E/mypck(17369):     at libcore.io.IoBridge.open(IoBridge.java:393)
07-15 14:31:07.035: E/mypck(17369):     ... 10 more

Related Code:

Class relating to exporting the file:

public class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {

    //Default constructor
    public ExportDatabaseFileTask() {

    }

    //delete if necessary
    //private final ProgressDialog dialog = new ProgressDialog(null);


    // can use UI thread here
    protected void onPreExecute() {
//      this.dialog.setMessage("Exporting database...");
//      this.dialog.show();
    }

    // automatically done on worker thread (separate from UI thread)
    protected Boolean doInBackground(final String... args) {

        //original database file location
        File dbFile = new File(Environment.getDataDirectory()
                + "/com.example.multapply/databases/MultapplyDatabase.db");

        //the destination file location
        File exportDir = new File(Environment.getExternalStorageDirectory(), "");
        if (!exportDir.exists()) {
            exportDir.mkdirs();
        }


        File file = new File(exportDir, dbFile.getName());
        try {
            file.createNewFile();
            this.copyFile(dbFile, file);
            return true;
        } catch (IOException e) {
            Log.e("mypck", e.getMessage(), e);
            return false;
        }
    }

    // can use UI thread here
    protected void onPostExecute(final Boolean success) {
//      if (this.dialog.isShowing()) {
//          this.dialog.dismiss();
//      }
//      if (success) {
//          Toast.makeText( null, "Export successful!", Toast.LENGTH_SHORT)
//                  .show();
//      } else {
//          Toast.makeText(null, "Export failed", Toast.LENGTH_SHORT).show();
//      }
    }

    void copyFile(File src, File dst) throws IOException {
        FileChannel inChannel = new FileInputStream(src).getChannel();
        FileChannel outChannel = new FileOutputStream(dst).getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } finally {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }
    }

}

Code where this class is instantiated and called:

/**
             * CRUD Operations
             * */
            // Inserting Contacts
            Log.d("Insert: ", "Inserting ..");

            db.addScore(new Score(UserName.getUserName(), score, System.currentTimeMillis() ));

            //attempting to export the file to the sd card
            ExportDatabaseFileTask task = new ExportDatabaseFileTask();
            task.execute();

Note: I was previously getting than error relating to "Writing exception to parcel" that I asked about here, but this error has gone now that I removed:

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

from the manifest.

Community
  • 1
  • 1
RYJava2014
  • 81
  • 3
  • 9

3 Answers3

0

Don't use Environment.getDataDirectory() to get your data location. Use your Context object to get your database and/or data location(s). See the methods getDir(), getDatabasePath(), and getExternalFilesDir().

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • Thanks for you help, sorry I am not exactly sure what you mean? Do you think this is what is causing the error? – RYJava2014 Jul 15 '14 at 13:47
  • I think @Larry tells is that Environment.getDataDirectory() could point to a wrong location which cannot be accessed by you. – RvdK Jul 15 '14 at 13:48
  • Thanks, I think that is correct as I read that on another question.What location should I be pointing to then for each file? – RYJava2014 Jul 15 '14 at 13:51
  • Yes, that is correct. `Environment.getDataDirectory()` is giving you the top level data directory, /data. Your app's data will actually be in /data/data/com.example.multapply for a single user device (phone). On a multi-user device (tablet) it will be in difference locations depending on the (human) user using the device. – Larry Schiefer Jul 15 '14 at 13:51
  • OK thanks, I think I am just having trouble with the two file paths I.e. the original location of the file and the location in which I want to copy it to. Can you post an edit so that I can adjust the two file paths? – RYJava2014 Jul 15 '14 at 13:58
  • 1
    The more important point, is don't **assume** what the private directory path will be, query it from the api, such as getFilesDir() – Chris Stratton Jul 15 '14 at 14:24
  • Thanks, but how would I do so for this specifc code> – RYJava2014 Jul 15 '14 at 15:02
0

If you copy the DB file to the SD card, for example to: /sdcard/MyApp/Databases/MultapplyDatabase.db, you need to open the file from that location.

File dbFile = new File("/sdcard/MyApp/Databases/MultapplyDatabase.db");
selalerer
  • 3,766
  • 2
  • 23
  • 33
  • Thank, however I am trying to open the file in DDMS at that location and no file is there? – RYJava2014 Jul 15 '14 at 13:52
  • You wrote you transferred the file to your SD card, but your code searches for the file in a different location. I don't see how DDMS has anything to do with it. – selalerer Jul 15 '14 at 13:56
  • OK thanks, I think I am just having trouble with the two file paths I.e. the original location of the file and the location in which I want to copy it to. Can you post an edit so that I can adjust the two file paths? – RYJava2014 Jul 15 '14 at 13:59
0

Add the following in your manifest application.

android:requestLegacyExternalStorage="true"
Umer Khalid
  • 330
  • 2
  • 16