我正在使用 SimpleDiskCache 代码 ( github link ) 为我正在使用的 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)
有人知道我做错了什么吗? 谢谢!
最佳答案
似乎输入流在使用之前就被关闭了。您必须在第 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();
}
}
关于android - SimpleDiskCache inputStream 数字格式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17382065/