Android | How Read file to byte array?

Viewed 5594

I need to sent file from phone to my device (HC-06 on board). But i need to send it part by part. How i can read file to byte array? I have my own transfer protocol..

2 Answers

This code worked for me, in an android project so hopefully, it will work for you.

byte[] fileContent = getByte(path); // call the method from there....


     private byte[] getByte(String path) {
        byte[] getBytes = {};
        try {
            File file = new File(path);
            getBytes = new byte[(int) file.length()];
            InputStream is = new FileInputStream(file);
            is.read(getBytes);
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return getBytes;
    }
Related