0

    public void copyDbToInternalFolder() throws IOException {

        String DB_PATH = Environment.getDataDirectory().getPath()+getPackageName()+"/Databases/"; // Don't worry I know this line doesn't work, however, nothing was working
        String DB_NAME = "Client_Responses.db";


        InputStream myInput = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS,"Client_Responses.db"); //needs to get database file from Downloads folder
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        myOutput.flush();
        myOutput.close();
        myInput.close();


    }

I have written several related apps that create sqlite database files in the devices Downloads folder. I need to stick to this method in order to support all the way back to first generation tablets. API 8 in essence.

Now, the manager needs to import found files from the Downloads folder and import them to its own database folder.

The above code doesn't work, null pointer exceptions and incorrect filename/pathname create errors and the app will force quit, and I have tried what seems like everything. My thinking is now bottlenecked.

I have been struggling with this and would welcome input.

Note: I will most likely duplicate the code in different class files, each copying a differing database name.

  • I have been working on an alternative using fileutils under Apache commons, as per http://stackoverflow.com/questions/19409137/android-copying-files-from-one-directory-to-another – NormalUser Feb 18 '16 at 21:08

1 Answers1

0
private void copyDbToInternalFolder() throws IOException {

    String ToDB_PATH = "/data/data/"+this.getPackageName()+"/databases/";

    String sourcePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/Client_Responses.db";
    File source = new File(sourcePath);

    String destinationPath = ToDB_PATH + "/" + DB_NAME;
    File destination = new File(destinationPath);
    try
    {
        FileUtils.copyFile(source, destination);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

This seems to work on Android4.2.2 tablet but not on the emulator. I cannot work out whether the issue is the emulator and how it handles the path creation, or if its API8 that cannot understand them. If anyone has any suggestions I would be grateful. So the issue is only partially answered.