0

My Android app downloads a bunch of photos and videos from a server and I want to cache this data. I've used DiskLruCach library to cache the images and it works fine but now I want to cache the videos also.

I've tried something like this but it doesn't seem to work - I can't find anything in the cache directory for the videos:

private boolean writeVideoToFile(String videoUri, DiskLruCache.Editor editor ) throws IOException, FileNotFoundException {
    OutputStream out = null;
    FileOutputStream fos;
    try {
        out = new BufferedOutputStream( editor.newOutputStream(0), Utils.IO_BUFFER_SIZE );
        File videoFile = Utils.createFile(Utils.TYPE_VIDEO_FILE);
        fos = new FileOutputStream(videoFile);
        fos.write(videoUri.getBytes());
        fos.close();
        return true;
    } finally {
        if ( out != null ) {
            out.close();
        }
    }
}

Can anyone give me an ideea on how I can accomplish this?

Alin
  • 1,044
  • 6
  • 20
  • 42

2 Answers2

1

Not enough info, but I guess the call to getBytes() is the problem, probably an OutOfMemory exception.

Don't read the entire video file into memory (calling getBytes). Use a small intermediate buffer instead, writing/caching the video file chunk by chunk.

Streets Of Boston
  • 12,576
  • 2
  • 25
  • 28
0

You are calling getBytes() for String videoUri. Is that really what you meant to do?

Anton Rutkevich
  • 592
  • 1
  • 7
  • 11
  • I've abandoned this code! Right now I'm working with a simple implementation of DiskLruCache. It works for bitmaps but I'm having problems with the video part. Please see this link - http://stackoverflow.com/questions/17382065/simplediskcache-inputstream-bad-number-format If you have any ideea please share! Thanks! – Alin Jun 30 '13 at 08:34