从asset复制数据库到data/data/databases

private File copyDB(){
    AssetManager asset =getAssets();
    InputStream inputStream=null;
    FileOutputStream outputStream=null;
    File file = new File("data/data/" + getPackageName() + "/databases");
    File dir=null;
    if(file.exists()||file.mkdirs()){
        dir=new File(file,"db_star.db");
    }

    try {
        inputStream=getAssets().open("db_star.db");
        outputStream=new FileOutputStream(dir);
        byte[] buffer=new byte[1024*2];
        int len=0;
        while((len=inputStream.read(buffer))!=-1){
            outputStream.write(buffer,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        try {
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return file;
}

猜你喜欢

转载自blog.csdn.net/ange_li/article/details/50511773