如何在缓存android中保存下载的文件

mad*_*erz 6 android

嗨我在我的Android应用程序中从一个网站流式传输视频.我有一个历史选项显示上次看过的视频.我想知道我是否可以使用缓存,以便当用户输入历史记录时,视频播放速度更快(不再下载).在Android中使用缓存是否意味着整个视频已下载并保存在某处?或者某些数据保存在某个地方(不是整个视频).

一些帮助将不胜感激!

谢谢.

gul*_*ndr 21

它应该对你有所帮助.

    URLConnection cn = new URL(mediaUrl).openConnection();   
    cn.connect();   
    InputStream stream = cn.getInputStream();

    File downloadingMediaFile = new File(context.getCacheDir(), "downloadingMedia.dat");

    FileOutputStream out = new FileOutputStream(downloadingMediaFile);   
    byte buf[] = new byte[16384];
    do {
        int numread = stream.read(buf);   
        if (numread <= 0) break;   
        out.write(buf, 0, numread);
        // ... 
    } while (...);
Run Code Online (Sandbox Code Playgroud)

  • 但是getCacheDir()返回一个`File`而不是`String`. (3认同)