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

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

作者: MemetGhini | 来源:发表于2023-03-15 16:50 被阅读0次

        前段时间开发过程中遇到处理完某个分辨率相机数据后在别人机器上花屏,在我本地不花屏的问题。因此为了想完美还原对方的数据,决定把相机的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);
        }
    }
    

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/asglrdtx.html