okhttp 没有下载文件
okhttp not downloading the file
我正在使用 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;
}
进度显示正确,但目录中未显示视频。
谢谢。
所以我的代码没有问题,但是路径有问题。感谢@greenapps 让我想到了 path/ 目录。
基本上我只是将路径更改为真实路径而不是缓存,是的,这是视频。
更改了这一行
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 提供的线索。
我正在使用 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;
}
进度显示正确,但目录中未显示视频。
谢谢。
所以我的代码没有问题,但是路径有问题。感谢@greenapps 让我想到了 path/ 目录。
基本上我只是将路径更改为真实路径而不是缓存,是的,这是视频。
更改了这一行
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 提供的线索。