1

我正在使用 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: read failed: EBADF (Bad file number)。这是堆栈跟踪:

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)

有谁知道我做错了什么?谢谢!

4

1 回答 1

2

似乎输入流在使用之前已关闭。您必须在第 59 行注释 snapshot.close() 调用,并在完成后自己关闭输入流。

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();
    }
}
于 2013-07-29T04:00:09.990 回答