FileInputStream to byte array in Android application

前端 未结 4 1245
梦如初夏
梦如初夏 2020-12-17 16:00

I have a FileInputStream created using Context.openFileInput(). I now want to convert the file into a byte array.

Unfortunately, I can\'t determine the

4条回答
  •  醉酒成梦
    2020-12-17 16:10

    This should work.

    InputStream is = Context.openFileInput(someFileName);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] b = new byte[1024];
    while ((int bytesRead = is.read(b)) != -1) {
       bos.write(b, 0, bytesRead);
    }
    byte[] bytes = bos.toByteArray();
    

提交回复
热议问题