-->

SimpleDiskCache的inputStream坏数字格式(SimpleDiskCache i

2019-10-18 07:52发布

我使用SimpleDiskCache代码( GitHub的链接 )来缓存几个视频文件到硬盘的Android应用我的工作。 以下是我把视频文件缓存:

OutputStream  fil =  videoCache.openStream(newData.getObjectId().toLowerCase());
fil.write(videoInBytes);
fil.flush();
fil.close();

这里就是我想从缓存中获取视频文件的代码:

InputStream in = videoCache.getInputStream(newData.getObjectId().toLowerCase()).getInputStream();
File videoFile = Utils.createFile(Utils.TYPE_VIDEO_FILE);
OutputStream os = new FileOutputStream(videoFile);
IOUtils.copy(in, os);
os.close();
in.close();

唯一的问题是,我得到一个IOExption:读失败:EBADF(错误的文件数)。 这里的堆栈跟踪:

06-29 18:47:21.422: W/System.err(19393): java.io.IOException: read failed: EBADF (Bad file number)
06-29 18:47:21.422: W/System.err(19393):    at libcore.io.IoBridge.read(IoBridge.java:442)
06-29 18:47:21.430: W/System.err(19393):    at java.io.FileInputStream.read(FileInputStream.java:179)
06-29 18:47:21.430: W/System.err(19393):    at java.io.InputStream.read(InputStream.java:163)
06-29 18:47:21.430: W/System.err(19393):    at com.google.api.client.util.ByteStreams.copy(ByteStreams.java:51)
06-29 18:47:21.430: W/System.err(19393):    at com.google.api.client.util.IOUtils.copy(IOUtils.java:87)
06-29 18:47:21.430: W/System.err(19393):    at com.google.api.client.util.IOUtils.copy(IOUtils.java:56)
06-29 18:47:21.430: W/System.err(19393):    at com.licenta.mementoapp.datafragments.VideoFragment$1.done(VideoFragment.java:151)

没有人有任何IDEEA我做错了什么? 谢谢!

Answer 1:

看来,输入流则在使用前关闭。 你必须在注释行59 snapshot.close()调用和自己关闭InputStream时,即可大功告成。

public InputStreamEntry getInputStream(String key) throws IOException {
    DiskLruCache.Snapshot snapshot = diskLruCache.get(toInternalKey(key));
    if (snapshot == null)
        return null;

    try {
        return new InputStreamEntry(snapshot, readMetadata(snapshot));
    } finally {
        //snapshot.close();
    }
}


文章来源: SimpleDiskCache inputStream bad number format