简体   繁体   English

将 int 数组(图像数据)写入文件的最佳方法是什么

[英]What is the best way to write an int array (image data) to file

I have a large int array containing image data in the format ARGB (alpha, R, G and B channels, 1 byte each).我有一个包含 ARGB 格式图像数据的大型 int 数组(alpha、R、G 和 B 通道,每个通道 1 个字节)。 I want to save it to file in onPause() to be able to reload it when the app is restarted.我想将它保存到onPause()文件,以便在重新启动应用程序时能够重新加载它。 What do you think is the best way to do that?你认为最好的方法是什么?

I found the following methods:我找到了以下方法:

  • Convert the int array to a byte array manually (see here ) and then use a FileOutputStream to output the byte array.手动将 int 数组转换为字节数组(参见此处),然后使用FileOutputStream输出字节数组。

  • Wrap the array into a java.nio.IntBuffer and then write the object to file using java.io.ObjectOutputStream.writeObject() .将数组包装到java.nio.IntBuffer ,然后使用java.io.ObjectOutputStream.writeObject()将对象写入文件。

  • Write each element one at a time using java.io.ObjectOutputStream.writeInt() .使用java.io.ObjectOutputStream.writeInt()一次写入一个元素。

All these methods seem quite wasteful so there is probably another, better way.所有这些方法看起来都很浪费,所以可能还有另一种更好的方法。 Possibly even a way to use image compression to reduce the size of the file?甚至可能是一种使用图像压缩来减小文件大小的方法?

From my point of view you can also use android specific storages从我的角度来看,您还可以使用 android 特定的存储

  1. Use database/content provider for storing image data使用数据库/内容提供程序存储图像数据
  2. Use out Bundle in onSaveInstance methodonSaveInstance方法中使用 out Bundle

If your still want to write to a file you can use following NIO based code:如果您仍然想写入文件,您可以使用以下基于 NIO 的代码:

static void writeIntArray(int[] array) throws IOException {
    FileOutputStream fos = new FileOutputStream("out.file");
    try {
        ByteBuffer byteBuff = ByteBuffer.allocate((Integer.SIZE / Byte.SIZE) * array.length);
        IntBuffer intBuff = byteBuff.asIntBuffer();
        intBuff.put(array);
        intBuff.flip();
        FileChannel fc = fos.getChannel();
        fc.write(byteBuff);
    } finally {
        fos.close();
    }
}

None of those.这些都没有。 Some of them don't even make sense.其中一些甚至没有意义。

DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

then call dos.writeInt() as many times as necessary, then close dos .然后根据需要多次调用dos.writeInt() ,然后关闭dos The buffer will take away most of the pain.缓冲区将消除大部分痛苦。

Or else create an IntBuffer and use FileChannel.write() , but I've never been able to figure out how that works in the absence of an IntBuffer.asByteBuffer() method.或者创建一个IntBuffer并使用FileChannel.write() ,但我一直无法弄清楚在没有IntBuffer.asByteBuffer()方法的情况下它是如何工作的。 Or else create a ByteBuffer , take it as an IntBuffer via asIntBuffer() , put the data in, then adjust the ByteBuffer 's length, which is another missing piece of the API, and again use FileChannel.write(ByteBuffer) .或者创建一个ByteBuffer ,通过asIntBuffer()将其作为IntBuffer ,将数据放入,然后调整ByteBuffer的长度,这是 API 的另一个缺失部分,再次使用FileChannel.write(ByteBuffer)

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

相关问题 Java:将对象数据成员写入文件的最佳方法是什么? - Java: What is the best way to write object data members to a file? 用Java写入文件的最佳方法是什么? - What is the best way to write to a file in Java? 不连续地在Java中写入文件的最佳方法是什么? - what is the best way to write to a file in java uncontinuously? 编写数据加载器的最佳方法是什么? - What is the best way to write a data loader? 使用 Android 存储访问框架编写包含数据的文件的最佳方法是什么? - what is the best way to write a file with data using Android Storage Access Framework? 在java中编写和附加大文件的最佳方法是什么 - What is the best way to write and append a large file in java 在 Java 的并行线程中写入文件的最佳方法是什么? - What is the best way to write to a file in a parallel thread in Java? 将uint32数据从hdf5文件写入Java中的数组的最简单方法是什么? - What is the easiest way to write uint32 data from a hdf5 file to an array in java? 将海量数据写入excel文件的Java最佳方法 - java best way to write huge data to excel file 从平面文件读取数据并将其写入xml的最佳方法 - best way to read data from flat file and write it to xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM