简体   繁体   中英

How to get File object from response body in OkHttp?

I download videos using OkHttp in my application. To play the video, I need to download it completely and then play it using a File object.

I couldn't find a good way to get the File object from the underlying DiskLruCache in OkHttp. Right now I use the InputStream of response body to pipe the response in another DiskLruCache in order to receive a File object and play it.

I think it's not a good idea to have multiple DiskLruCaches. Is there any good way to get a File object from the cached response in OkHttp?

You can't, and you shouldn't want to anyway. The problem is that OkHttp's disk cache may evict files as you're using them. It doesn't have any mechanism to pin a file for a purpose other than its own.

Instead you need to manage the lifecycle of these downloads manually: how many to store, when to delete them, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM