Resources.openRawResource() issue Android

后端 未结 2 2065
终归单人心
终归单人心 2020-11-30 10:21

I have a database file in res/raw/ folder. I am calling Resources.openRawResource() with the file name as R.raw.FileName and I get an

2条回答
  •  -上瘾入骨i
    2020-11-30 11:03

    Yes, you should be able to use openRawResource to copy a binary across from your raw resource folder to the device.

    Based on the example code in the API demos (content/ReadAsset), you should be able to use a variation of the following code snippet to read the db file data.

    InputStream ins = getResources().openRawResource(R.raw.my_db_file);
    ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
    int size = 0;
    // Read the entire resource into a local byte buffer.
    byte[] buffer = new byte[1024];
    while((size=ins.read(buffer,0,1024))>=0){
      outputStream.write(buffer,0,size);
    }
    ins.close();
    buffer=outputStream.toByteArray();
    

    A copy of your file should now exist in buffer, so you can use a FileOutputStream to save the buffer to a new file.

    FileOutputStream fos = new FileOutputStream("mycopy.db");
    fos.write(buffer);
    fos.close();
    

提交回复
热议问题