Failed to open database/ Failed to change locale for (database) to 'en_US'

前端 未结 4 877
误落风尘
误落风尘 2021-01-20 03:36

I have read solution from Failed to change locale for db '/data/data/my.easymedi.controller/databases/EasyMediInfo.db' to 'en_US' but it doesnt help me. I st

4条回答
  •  一整个雨季
    2021-01-20 03:41

    The problem that you get is that the file cannot be opened. Probably its something wrong with your path (it could be slightly different between diferent android versions), and anyways, you should never hardcode it.

    You can get database path with context.getDatabasePath(); you pass the desired name to the file (no matter if it exists or not). And actually you are supposed to do it that way

    For that, at any place you are using something like String outFileName = DB_PATH + DB_NAME; you should change it for

     String outFileName =myContext.getDatabasePath(DB_NAME).getPath() ;
    

    and that's the real valid location for your file. so , your copyDataBase() will be like this

     private void copyDataBase() throws IOException {
    
        // Open your local db as the input stream
        InputStream myInput = context.getAssets().open("db/" + DATABASE_NAME);
    
        // Path to the just created empty db
         String outFileName =myContext.getDatabasePath(DB_NAME).getPath() ;
    
        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);
    
        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
    
        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    
    }
    

    and the same change in your private boolean checkDataBase()

提交回复
热议问题