okhttp 没有下载文件 [英] okhttp not downloading the file

查看:187
本文介绍了okhttp 没有下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 okhttp 从服务器下载视频.没有错误也不例外,但文件并未在每个地方下载,但看起来确实如此.

I am using okhttp for downloading video from server. there is no error no exception but the file is not downloading every where but it seems as it is.

代码如下:

OkHttpClient httpClient = new OkHttpClient();
Call call = httpClient.newCall(new Request.Builder().url("http://res.cloudinary.com/demo/video/upload/v1427018743/ygzxwxmflekucvqcrb8c.mp4").get().build());
 try {
        File file = new File(getCacheDir(), user_Videos.get(i).video_title+ ".mp4");
        OutputStream out = new FileOutputStream(file);
        Response response = call.execute();
        if (response.code() == 200) {
           InputStream inputStream = null;
           try {
                inputStream = response.body().byteStream();
                byte[] buff = new byte[1024 * 8];
                long downloaded = 0;
                long target = response.body().contentLength();

                 publishProgress(0L, target);
                 while (true) {
                      int readed = inputStream.read(buff);
                       if (readed == -1) {
                           break;
                                        }
                          //write buff
                     downloaded += readed;

              try {
                   out.write(buff,0,readed);

                  } catch (Exception e) {
                       e.printStackTrace();
                                        }
                   publishProgress(downloaded, target);
                     if (isCancelled()) {
                                            return false;
                                        }
                                    }
                                    return downloaded == target;
                                } catch (IOException ignore) {
                                    return false;
                                } finally {
                                    if (inputStream != null) {
                                        out.flush();
                                        out.close();
                                        inputStream.close();
                                    }
                                }
                            } else {
                                return false;
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                            return false;
                        }

进度显示正确,但目录中未显示视频.

The progress is showing correctly but video is not showing in directory.

谢谢.

推荐答案

所以我的代码没有问题,只是路径问题.感谢@greenapps 让我想到 path/目录.

So there is no problem in my code but the path. Thanks @greenapps who made me think about path/ directory.

基本上我只是将路径更改为真实路径而不是缓存,是的,这里是视频.

Basically I just change the path to a real one instead of cache and yup here is the video.

改变这一行

 File file = new File(getCacheDir(), user_Videos.get(i).video_title  + ".mp4");

到这里

 File file = new File(Environment.getExternalStorageDirectory(), user_Videos.get(i).video_title+ ".mp4");

感谢@greenapps 提供线索.

Thanks @greenapps for the clue.

这篇关于okhttp 没有下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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