1

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);
                    //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.

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

4 Comments

This works, but only if I use lengthName+3 instead of lengthName. Any reason why this might be happening ?
When you're getting number of images I think you should use numImages = ds.readInt(). Maybe this is the reason.
I have tried this and it does not work. It crashes the application.And sometimes it returns random values which are unrelated to the data that is saved. Could this be a byte order problem?
I think it is. You can try to use android.nio.ByteBuffer to change endianness. Just set it's order ByteBuffer.order(ByteOrder.LITTLE_ENDIAN)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.