如何将下载的文件保存在Android缓存中 [英] how to save downloaded files in cache android

查看:172
本文介绍了如何将下载的文件保存在Android缓存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi i'm streaming video from a website in my android application. I have a history option showing the last seen videos. I wonder if i can use cache so that when the user enters the history the video is played faster (not downloaded again). When you use cache in Android does that mean that the whole video is downloaded and saved somewhere? or some data is saved somwhere(not the whole video).

一些帮助将不胜感激!

谢谢.

推荐答案

它应该对您有所帮助.

    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 (...);

这篇关于如何将下载的文件保存在Android缓存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆