23
23
import com .hjq .easy .demo .http .model .HttpData ;
24
24
import com .hjq .http .EasyHttp ;
25
25
import com .hjq .http .EasyUtils ;
26
- import com .hjq .http .exception .FileMd5Exception ;
27
26
import com .hjq .http .listener .HttpCallbackProxy ;
28
27
import com .hjq .http .listener .OnDownloadListener ;
29
28
import com .hjq .http .listener .OnUpdateListener ;
@@ -164,21 +163,23 @@ public void onHttpSuccess(HttpData<SearchBlogsApi.Bean> result) {
164
163
return ;
165
164
}
166
165
166
+ /*
167
167
// 如果是放到外部存储目录下则需要适配分区存储
168
- // String fileName = "EasyHttp.png";
169
- // File file;
170
- // Uri outputUri;
171
- // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
172
- // // 适配 Android 10 分区存储特性
173
- // ContentValues values = new ContentValues();
174
- // // 设置显示的文件名
175
- // values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
176
- // // 生成一个新的 uri 路径
177
- // outputUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
178
- // file = new FileContentResolver(getContentResolver(), outputUri, fileName);
179
- // } else {
180
- // file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);
181
- // }
168
+ String fileName = "EasyHttp.png";
169
+ File file;
170
+ Uri outputUri;
171
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
172
+ // 适配 Android 10 分区存储特性
173
+ ContentValues values = new ContentValues();
174
+ // 设置显示的文件名
175
+ values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
176
+ // 生成一个新的 uri 路径
177
+ outputUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
178
+ file = new FileContentResolver(getContentResolver(), outputUri, fileName);
179
+ } else {
180
+ file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);
181
+ }
182
+ */
182
183
183
184
// 如果是放到外部存储的应用专属目录则不需要适配分区存储特性
184
185
File file = new File (getExternalFilesDir (Environment .DIRECTORY_PICTURES ), "我是测试专用的图片.png" );
@@ -236,22 +237,27 @@ public void onUpdateEnd(Call call) {
236
237
return ;
237
238
}
238
239
240
+ /*
239
241
// 如果是放到外部存储目录下则需要适配分区存储
240
- // String fileName = "微信 8.0.15.apk";
241
- //
242
- // File file;
243
- // Uri outputUri;
244
- // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
245
- // // 适配 Android 10 分区存储特性
246
- // ContentValues values = new ContentValues();
247
- // // 设置显示的文件名
248
- // values.put(MediaStore.Downloads.DISPLAY_NAME, fileName);
249
- // // 生成一个新的 uri 路径
250
- // outputUri = getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values);
251
- // file = new FileContentResolver(getContentResolver(), outputUri, fileName);
252
- // } else {
253
- // file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
254
- // }
242
+ String fileName = "微信 8.0.15.apk";
243
+
244
+ File file;
245
+ Uri outputUri;
246
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
247
+ // 适配 Android 10 分区存储特性
248
+ ContentValues values = new ContentValues();
249
+ // 设置显示的文件名
250
+ values.put(MediaStore.Downloads.DISPLAY_NAME, fileName);
251
+ // 生成一个新的 uri 路径
252
+ // 注意这里使用 ContentResolver 插入的时候都会生成新的 Uri
253
+ // 解决方式将 ContentValues 和 Uri 作为 key 和 value 进行持久化关联
254
+ // outputUri = getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values);
255
+ outputUri = ContentResolverUriStore.insert(this, Downloads.EXTERNAL_CONTENT_URI, values);
256
+ file = new FileContentResolver(getContentResolver(), outputUri, fileName);
257
+ } else {
258
+ file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
259
+ }
260
+ */
255
261
256
262
// 如果是放到外部存储的应用专属目录则不需要适配分区存储特性
257
263
File file = new File (getExternalFilesDir (Environment .DIRECTORY_DOWNLOADS ), "微信 8.0.15.apk" );
@@ -262,6 +268,8 @@ public void onUpdateEnd(Call call) {
262
268
//.url("https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk")
263
269
.url ("https://dldir1.qq.com/weixin/android/weixin8015android2020_arm64.apk" )
264
270
.md5 ("b05b25d4738ea31091dd9f80f4416469" )
271
+ // 设置断点续传(默认不开启)
272
+ .resumableTransfer (true )
265
273
.listener (new OnDownloadListener () {
266
274
267
275
@ Override
@@ -284,10 +292,7 @@ public void onDownloadSuccess(File file) {
284
292
@ Override
285
293
public void onDownloadFail (File file , Throwable throwable ) {
286
294
Toaster .show ("下载失败:" + throwable .getMessage ());
287
- if (throwable instanceof FileMd5Exception ) {
288
- // 如果是文件 md5 校验失败,则删除文件
289
- file .delete ();
290
- }
295
+ file .delete ();
291
296
}
292
297
293
298
@ Override
0 commit comments