Skip to content

Commit 83f2249

Browse files
committed
Improve demo downloading
1 parent 4e4d09f commit 83f2249

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

app/src/main/java/me/jessyan/progressmanager/demo/MainActivity.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.bumptech.glide.Glide;
1414
import com.bumptech.glide.load.engine.DiskCacheStrategy;
1515

16+
import java.io.BufferedInputStream;
1617
import java.io.File;
1718
import java.io.FileOutputStream;
1819
import java.io.IOException;
@@ -31,7 +32,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
3132
private static final String TAG = "MainActivity";
3233
// github 服务器可能下载不稳定
3334
public static final String IMAGE_URL = "https://raw.githubusercontent.com/JessYanCoding/MVPArmsTemplate/master/art/step.png";
34-
public static final String DOWNLOAD_URL = "https://raw.githubusercontent.com/JessYanCoding/MVPArmsTemplate/master/art/MVPArms.gif";
35+
public static final String DOWNLOAD_URL = "http://211.162.175.13/files/31170000025FB3A3/speed.myzone.cn/WindowsXP_SP2.exe";
3536
public static final String UPLOAD_URL = "http://upload.qiniu.com/";
3637

3738
private ImageView mImageView;
@@ -119,7 +120,7 @@ private ProgressListener getUploadListener() {
119120
public void onProgress(ProgressInfo progressInfo) {
120121
// 如果你不屏蔽用户重复点击上传或下载按钮,就可能存在同一个 Url 地址,上一次的上传或下载操作都还没结束,
121122
// 又开始了新的上传或下载操作,那现在就需要用到 id(请求开始时的时间) 来区分正在执行的进度信息
122-
// 这里我就取最新的上传操作用来展示,顺便展示下 id 的用法
123+
// 这里我就取最新的上传进度用来展示,顺便展示下 id 的用法
123124

124125
if (mLastUploadingingInfo == null) {
125126
mLastUploadingingInfo = progressInfo;
@@ -159,7 +160,7 @@ private ProgressListener getDownloadListener() {
159160
public void onProgress(ProgressInfo progressInfo) {
160161
// 如果你不屏蔽用户重复点击上传或下载按钮,就可能存在同一个 Url 地址,上一次的上传或下载操作都还没结束,
161162
// 又开始了新的上传或下载操作,那现在就需要用到 id(请求开始时的时间) 来区分正在执行的进度信息
162-
// 这里我就取最新的下载操作用来展示,顺便展示下 id 的用法
163+
// 这里我就取最新的下载进度用来展示,顺便展示下 id 的用法
163164

164165
if (mLastDownloadingInfo == null) {
165166
mLastDownloadingInfo = progressInfo;
@@ -171,7 +172,7 @@ public void onProgress(ProgressInfo progressInfo) {
171172
} else if (progressInfo.getId() > mLastDownloadingInfo.getId()) {
172173
mLastDownloadingInfo = progressInfo;
173174
}
174-
175+
// 如果getCurrentbytes 等于 -1 说明二进制已经读取完,可能是成功下载完所有数据,也可能是遭遇了错误
175176
int progress = (int) ((100 * mLastDownloadingInfo.getCurrentbytes()) / mLastDownloadingInfo.getContentLength());
176177
mDownloadProgress.setProgress(progress);
177178
mDownloadProgressText.setText(progress + "%");
@@ -215,7 +216,7 @@ private void uploadStart() {
215216
@Override
216217
public void run() {
217218
try {
218-
219+
//为了方便就不动态申请权限了,直接将文件放到CacheDir()中
219220
File file = new File(getCacheDir(), "a.java");
220221
//读取Assets里面的数据,作为上传源数据
221222
writeToFile(getAssets().open("a.java"), file);
@@ -247,7 +248,23 @@ private void downloadStart() {
247248
public void run() {
248249
try {
249250
Response response = mOkHttpClient.newCall(request).execute();
250-
System.out.println(response.body().string());
251+
252+
InputStream is = response.body().byteStream();
253+
//为了方便就不动态申请权限了,直接将文件放到CacheDir()中
254+
File file = new File(getCacheDir(), "download");
255+
FileOutputStream fos = new FileOutputStream(file);
256+
BufferedInputStream bis = new BufferedInputStream(is);
257+
byte[] buffer = new byte[1024];
258+
int len;
259+
while ((len = bis.read(buffer)) != -1) {
260+
fos.write(buffer, 0, len);
261+
}
262+
fos.flush();
263+
fos.close();
264+
bis.close();
265+
is.close();
266+
267+
251268
} catch (IOException e) {
252269
e.printStackTrace();
253270
}

app/src/main/java/me/jessyan/progressmanager/demo/MainFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private ProgressListener getUploadListener() {
9191
public void onProgress(ProgressInfo progressInfo) {
9292
// 如果你不屏蔽用户重复点击上传或下载按钮,就可能存在同一个 Url 地址,上一次的上传或下载操作都还没结束,
9393
// 又开始了新的上传或下载操作,那现在就需要用到 id(请求开始时的时间) 来区分正在执行的进度信息
94-
// 这里我就取最新的上传操作用来展示,顺便展示下 id 的用法
94+
// 这里我就取最新的上传进度用来展示,顺便展示下 id 的用法
9595

9696
if (mLastUploadingingInfo == null) {
9797
mLastUploadingingInfo = progressInfo;
@@ -131,7 +131,7 @@ private ProgressListener getDownloadListener() {
131131
public void onProgress(ProgressInfo progressInfo) {
132132
// 如果你不屏蔽用户重复点击上传或下载按钮,就可能存在同一个 Url 地址,上一次的上传或下载操作都还没结束,
133133
// 又开始了新的上传或下载操作,那现在就需要用到 id(请求开始时的时间) 来区分正在执行的进度信息
134-
// 这里我就取最新的下载操作用来展示,顺便展示下 id 的用法
134+
// 这里我就取最新的下载进度用来展示,顺便展示下 id 的用法
135135

136136
if (mLastDownloadingInfo == null) {
137137
mLastDownloadingInfo = progressInfo;

0 commit comments

Comments
 (0)