我们的眼睛就是我们的监狱,我们的眼光所到之处就是我们监狱的围墙。——尼采

首先请求授权

然后我们这里引入依赖

1
implementation 'com.hujiang.restvolley:restvolley:1.0.1'

然后调用函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
String filename = url.substring(url.lastIndexOf("/"));
String localPath = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) + filename;
RestVolleyDownload.download(context, url, localPath, new RestVolleyDownload.OnDownloadListener() {
@Override
public void onDownloadStart(String url) {

}

@Override
public void onDownloadSuccess(String url, File file, int httpCode, Headers headers) {
ToastUtils.shortToast("保存成功,文件地址:" + localPath);
}

@Override
public void onDownloadFailure(String url, Exception e, int httpCode, Headers headers) {
ToastUtils.shortToast("保存失败");
}

@Override
public void onDownloadProgress(String url, int downloadBytes, int contentLength, File file, int httpCode, Headers headers) {

}
});

就能实现根据网络图片url下载图片到本地啦!

源码地址