从无根设备资产的文件夹复制数据库 [英] Copy Database from assets folder in unrooted device

查看:201
本文介绍了从无根设备资产的文件夹复制数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从资产的文件夹复制DB到设备。这code工作正常的模拟器和植根设备。我只是想知道的是它创建于无根的设备上的任何问题,或将工作一样。

 私人无效StoreDatabase(){
    文件DBFILE =新的文件(
            数据/数据​​/软件包名/ DBname.sqlite);
    如果(DbFile.exists()){
        的System.out.println(文件已经存在,没有必要创建);
    } 其他 {
        尝试 {
            DbFile.createNewFile();
            的System.out.println(文件创建成功);
            InputStream的是= this.getAssets()开(DBname.sqlite)。
            FileOutputStream中FOS =新的FileOutputStream(DBFILE);
            byte []的缓冲区=新的字节[1024];
            INT长度= 0;
            而((长度= is.​​read(缓冲液))大于0){
                fos.write(缓冲液,0,长度);
            }
            的System.out.println(文件成功地放置在SD卡);
            //关闭流
            fos.flush();
            fos.close();
            is.close();
        }赶上(IOException异常E){
            e.printStackTrace();
        }
    }

}
 

解决方案

这将工作一定能在所有的设备和仿真器,无须根。

  / **
     *复制你的数据库从当地的资产文件夹复制到刚创建
     *在系统文件夹,清空数据库,从那里可以访问和
     *处理。这是通过transfering字节流进行。
     * * /
    私人无效copyDataBase(字符串数据库名)抛出IOException异常{
        //打开本地数据库作为输入流
        InputStream的myInput = myContext.getAssets()开(数据库)。
        //路径刚刚创建的空分贝
        字符串outFileName = getDatabasePath(数据库);
        //打开空分贝的输出流
        的OutputStream myOutput =新的FileOutputStream(outFileName);
        //传输的字节从inputfile中的OUTPUTFILE
        byte []的缓冲区=新的字节[1024];
        INT长;
        而((长度= myInput.read(缓冲液))大于0){
            myOutput.write(缓冲液,0,长度);
        }
        //关闭流
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }
 

I am trying to copy DB from assets folder to device. This code is working fine on Emulator and rooted Device. I just want to know is it create any problem on unrooted device or it will work same.

private void StoreDatabase() {
    File DbFile = new File(
            "data/data/packagename/DBname.sqlite");
    if (DbFile.exists()) {
        System.out.println("file already exist ,No need to Create");
    } else {
        try {
            DbFile.createNewFile();
            System.out.println("File Created successfully");
            InputStream is = this.getAssets().open("DBname.sqlite");
            FileOutputStream fos = new FileOutputStream(DbFile);
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = is.read(buffer)) > 0) {
                fos.write(buffer, 0, length);
            }
            System.out.println("File succesfully placed on sdcard");
            // Close the streams
            fos.flush();
            fos.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

解决方案

This will work for sure in all devices and emulator , no need to root.

/**
     * Copies your database from your local assets-folder to the just created
     * empty database in the system folder, from where it can be accessed and
     * handled. This is done by transfering bytestream.
     * */
    private void copyDataBase(String dbname) throws IOException {
        // Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(dbname);
        // Path to the just created empty db
        String outFileName = getDatabasePath(dbname);
        // 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();
    }

这篇关于从无根设备资产的文件夹复制数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆