安卓开发如何把ByteBuffer存到文件,再从文件读到内存中?

2023-03-15  本文已影响0人  MemetGhini

    前段时间开发过程中遇到处理完某个分辨率相机数据后在别人机器上花屏,在我本地不花屏的问题。因此为了想完美还原对方的数据,决定把相机的YUV数据存成文件发过来,我这边再加载到内存这样就比较好复现定位问题。不多说废话直接上代码:

如何把ByteBuffer存到沙河目录里?

public void writeByteBufferToFile(ByteBuffer byteBuffer) {
    try {
        String dstPath = mContext.getFilesDir().getAbsolutePath() + "/rawData.yuv";
        FileChannel fc = new FileOutputStream(dstPath).getChannel();
        byteBuffer.position(0);
        fc.write(byteBuffer);
        fc.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

如何从Asset中的文件读到ByteBuffer中?

public void readByteBufferFromFile(ByteBuffer byteBuffer) {
    try {
        InputStream inputStream = mContext.getAssets().open("/rawData.yuv");
        int size = inputStream.available();
        byte[] buffer = new byte[size];
        inputStream.read(buffer);
        byteBuffer.put(buffer);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
上一篇下一篇

猜你喜欢

热点阅读