java - 在 Android 上读取二进制文件

标签 java android matlab

我有一些数据已使用 Matlab 保存到文件中。我已将这些数据保存在 Matlab 中,如下所示:

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'); 

我使用以下代码将这些数据读回 Matlab

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...

我希望能够在安卓手机上读取这些数据。这是我目前的代码。

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

我似乎无法从二进制文件数据中检索图像名称作为字符串。非常感谢任何帮助。

最佳答案

我不确定它是否会起作用,但无论如何:

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

关于java - 在 Android 上读取二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5735291/

相关文章:

java - 使用Java从文本文件中读取希伯来语

java - Spring找不到类路径中存在的类

android - 将Android App与本地机器上存储的Mysql数据库连接

java - 覆盖 getColumnClass 不适用于日期列

matlab - 在 matlab 中为对数图选择不同的基数

image - 为什么图像的红色,绿色,蓝色 channel 分别是灰度(Matlab)?

java - Guava 事件总线 : where to put it in GUI application?

android - 在 Android 中将服务用作单例

android - 我正在使用 PhoneGap 并且在我的页面中尝试使用 FontAwesome 但它不起作用?

Java 输入需要两次