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

时间:2011-01-14 10:48:20

标签: android

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

一些帮助将不胜感激!!!

感谢。

1 个答案:

答案 0 :(得分:20)

它可以帮助你。

    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 (...);
相关问题