0

我正在使用 okhttp 从服务器下载视频。没有错误也不例外,但文件并没有在每个地方下载,但它似乎是这样。

这是代码:

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;
                        }

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

谢谢。

4

1 回答 1

0

所以我的代码没有问题,但路径。感谢@greenapps 让我想到了路径/ 目录。

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

改变了这一行

 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 提供线索。

于 2016-04-12T05:38:45.893 回答