0

Is there a way to copy sqlite database from external storage to assets at run time and then use it ? or is there a way to copy the ready sqlite from external storage to below path at run time and then read it in app? context.getDatabasepath. I have problem with the copying process, please help me because I got this error :

can't open file

My question may look like a duplicate but I couldn't find the answer.

public void copyDataBase() throws IOException
    {
        try
        {

        String inputDB = Environment.getExternalStorageDirectory().getPath() + "/test.sqlite" ;
        File input = new File(inputDB);
        InputStream myInput = new FileInputStream(input);
        String outputFileName = databasePath + DATABASE_NAME ;
        OutputStream myoutput = new FileOutputStream(outputFileName) ;
        byte[] buffer = new byte[1024];
        int length;

        while((length = myInput.read(buffer))>0)
        {
            myoutput.write(buffer,0,length);

        }
        myoutput.flush();
        myoutput.close();
        myInput.close();
    }

    catch (Exception e)
    {
        e.printStackTrace();
    }
}

I think the file should be opened first then we give it to the input stream because error was like : can't open the file. Please help me.

G5W
  • 36,531
  • 10
  • 47
  • 80

0 Answers0