【问题标题】:okhttp not downloading the fileokhttp 没有下载文件
【发布时间】:2016-04-12 14:28:55
【问题描述】:

我正在使用 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;
                        }

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

谢谢。

【问题讨论】:

  • 您是否添加了写入存储的权限?
  • 是的,但没有运气。
  • video is not showing in directory.。你怎么看那个目录?该目录的完整路径是什么?
  • Have you added the permission to write to the storage。该目录不需要权限。
  • 这是文件路径:/data/data/com.rev.rev/cache/Gold -en.mp4 除了我也在其他地方搜索过。带有文件名。

标签: android video okhttp3


【解决方案1】:

所以我的代码没有问题,但路径。感谢@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 提供线索。

【讨论】:

  • 你不应该硬编码"/sdcard"。您还应该考虑删除 WRITE_EXTERNAL_STORAGE 权限,因为它是 Android 6.0+ 上的“危险”权限。
  • 改用Environment.getExternalStorageDirectory().getPath()。硬编码"/sdcard" 适用于大多数设备,但它很可能是符号链接,在某些设备上可能不存在。
  • 那些无法使用的设备呢?
  • 使用Environment.getExternalStorageDirectory()
猜你喜欢
  • 2016-09-12
  • 2014-11-11
  • 2017-05-27
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
相关资源
最近更新 更多