1.build.gradle中添加阿里云SDK
api'com.aliyun.dpa:oss-android-sdk:+'
【Android---阿里云网络视频带进度下载】2.接口
//系统相册目录
String galleryPath = Environment.getExternalStorageDirectory()
+ File.separator + Environment.DIRECTORY_DCIM
+ File.separator + "Camera";
File gallery = new File(galleryPath);
if (!FileUtils.isFileExists(gallery)) {
gallery.mkdir();
}
File file = new File(galleryPath, "视频名称.MP4");
/**
* @API GetObjectRequest
* 创建新请求以获取指定对象
* @参数 bucketName Bucket name
* @参数 objectKeyObject key
*/
GetObjectRequest request = new GetObjectRequest("阿里云API的bucket名称", "阿里云视频地址");
GetObjectResult object = null;
try { /**
* API:GetObject
* 同步下载文件
* 获取对象。(调用者需要该对象的读取权限)
* @参数 request
*/
object = oss.getObject(request);
} catch (Exception e) {
Log.e(TAG, e.toString());
}
if (object != null) {
InputStream input = null;
FileOutputStream uriOutputStream = null;
byte[] data;
long totalLength = object.getMetadata().getContentLength();
int nowLength = 0;
try {
/**
* API: getObjectContent
* 获取对象内容
* @返回 InputStream形式的对象内容
*/
input = object.getObjectContent();
data = https://www.it610.com/article/new byte[input.available()];
int len;
uriOutputStream = new FileOutputStream(file.toString());
while (true) {
len = input.read(data);
//这里设置ProgressBar的当前值(nowLength)和最大值(totalLength)
if (len < 0) break;
if (//如果需要取消下载,这里设置取消条件) {
return;
}
nowLength += data.length;
uriOutputStream.write(data, 0, data.length);
data = new byte[input.available()];
}
Tools.scanFile2(context, file.toString());
//刷新手机系统相册
} catch (IOException e) {
e.printStackTrace();
} finally {
if (uriOutputStream != null) {
try {
uriOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}