简体   繁体   English

Android:读取二进制文件失败

[英]Android : Read binary file failure

I was try to read a binary file storing the position of x, y, z ,w. 我试图读取一个存储x,y,z,w位置的二进制文件。 that is, they are all float such as 1.4567896 5.156986 .....etc. 也就是说,它们都是浮动的,例如1.4567896 5.156986 ..... etc。

I successfully read it in c++ and also java, but couldn't done in android, all I read in is all zero... 我在c ++和java中都成功读取了它,但是在android中却无法完成,我读到的全部都是零...

Here is my code : ps. 这是我的代码:ps。 I try many codes before, all failed... 我之前尝试过很多代码,但都失败了...

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;
    }
}

I blocked the transformation of byte to float cuz read in byte have to be check if they are correct first. 我阻止了将字节转换为浮点型cuz,必须先检查它们是否正确,然后再读取字节。

在Android上,您需要使用可运行的界面来不断检查变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM