问题
I'm using the SimpleDiskCache code (github link) to cache a few video files to disk for an Android app I'm working. Here's how I put the video file to cache:
OutputStream fil = videoCache.openStream(newData.getObjectId().toLowerCase());
fil.write(videoInBytes);
fil.flush();
fil.close();
And here's the code where I want to retrieve the video file from cache:
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();
The only problem is that I get a IOExption: read failed: EBADF (Bad file number). Here's the stack trace:
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)
Does anyone have any ideea what I'm doing wrong? Thanks!
回答1:
It seems that the input stream is closed before being used. You have to comment the snapshot.close() call at line 59 and close the inputstream yourself when you're done.
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();
}
}
来源:https://stackoverflow.com/questions/17382065/simplediskcache-inputstream-bad-number-format