Android:读取二进制文件失败

时间:2016-07-12 05:32:45

标签: java android binary

我试图读取存储x,y,z,w位置的二进制文件。 也就是说,它们都是浮动的,例如1.4567896 5.156986 .....等。

我用c ++和java成功读取了它,但是在android中无法完成, 我读到的全部都是零...

这是我的代码: PS。我之前尝试过很多代码,都失败了......

public class DataIO {

    private int             DataSize = 640*320*4;
    private float[]         fDataBuffer;
    private byte[]          bDataBuffer;

    private String          sOutput;

    public DataIO(){
        bDataBuffer = new byte[DataSize*4];
        fDataBuffer = new float[DataSize];
    }

    /*=============================================
    *   Function:
    *       ReadFile
    *   Description:
    *       Read binary float data to float[] buffer.
    *================================================ */
    public void ReadFile(String FileName){
        File file = new File(FileName);
        try {
            FileInputStream finStream = new FileInputStream(file);

            if(finStream.read(bDataBuffer) == -1){
                Log.d("ReadData", "Reading Data has problem.");
            }

            finStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        int index = 0;
        for(int i = 0; i < DataSize; i+=1){
            /*int asInt = (bDataBuffer[i+0] & 0xFF)
                    | ((bDataBuffer[i+1] & 0xFF) << 8)
                    | ((bDataBuffer[i+2] & 0xFF) << 16)
                    | ((bDataBuffer[i+3] & 0xFF) << 24);
            fDataBuffer[index] = Float.intBitsToFloat(asInt);*/

            sOutput += String.format("%d ", bDataBuffer[i]);
            index++;
        }

    }

    /*=============================================
    *   Function:
    *       GetData
    *   Description:
    *       output the data by string.
    *================================================ */
    public String GetData(){
        return sOutput;
    }
}
  

我阻止了字节到float的转换cuz读取字节必须首先检查它们是否正确。

1 个答案:

答案 0 :(得分:0)

在Android上,您需要使用runnable接口来不断检查变量。