开发者

Read From Binary File on Android

I have some data that I have saved into a file using Matlab. I have saved this data in Matlab as follows:

fwrite(fid,numImg2,'integer*4');
fwrite(fid,y,'integer*4');
fwrite(fid,imgName,'char*1');
fwrite(fid,a,'integer*4');        
fwrite(fid,img.imageData,'double'); 

I read this data back into Matlab using the following code

fread(fid,1,'integer*4');// Returns numImg2
fread(fid,1,'integer*4');// Returns y which is the number of cha rectors in the image name,     i use in the following line to read the image name, say for example if the image name is 1.jpg, then using the following will return the image name
fread(fid,5,'char*1');
fread(fid,1);
etc...

I want to be able to read this data on an android phone. This is the code I have at the moment.

DataInputStream ds = new DataInputStream(new FileInputStream(imageFile));
                //String line;
                // Read the first byte to find out how many images are stored in the file.
                int x = 0;
                byte numberOfImages;
                int numImages = 0;
                while(x<1)
                {
                    numberOfImages = ds.readByte();
                    numImages = (int)numberOfImages;
                    x++;
                }

                int lengthName = 0;
                String imgName = "";
                for(int y=1; y<=numImages; y++)
                {
                    lengthName = ds.readInt();
                    byte[] nameBuffer = new byte[lengthName];
                    char[] name = new char[lengthName];
                    for(int z = 1; z<=5;z++)
                    {
                        nameBuffer[z-1] = ds.readByte();
                        //name[z-1] = ds.readChar();
                    }
                    imgName = new String(nameBuffer);
  开发者_如何学Python                  //imgName = name.toString();
                }

                text.append(imgName);

I cannot seem to retrieve the image name as a string from the binary file data. Any help is much appreciated.


I'm not sure it will work but anyway:

byte[] nameBuffer = new byte[lengthName];
if(ds.read(nameBuffer) != lengthName) {
    // error handling here
    return;
}
imgName = new String(nameBuffer, "ISO-8859-1");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜