2323import com .hjq .easy .demo .http .model .HttpData ;
2424import com .hjq .http .EasyHttp ;
2525import com .hjq .http .EasyUtils ;
26- import com .hjq .http .exception .FileMd5Exception ;
2726import com .hjq .http .listener .HttpCallbackProxy ;
2827import com .hjq .http .listener .OnDownloadListener ;
2928import com .hjq .http .listener .OnUpdateListener ;
@@ -164,21 +163,23 @@ public void onHttpSuccess(HttpData<SearchBlogsApi.Bean> result) {
164163 return ;
165164 }
166165
166+ /*
167167 // 如果是放到外部存储目录下则需要适配分区存储
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+ */
182183
183184 // 如果是放到外部存储的应用专属目录则不需要适配分区存储特性
184185 File file = new File (getExternalFilesDir (Environment .DIRECTORY_PICTURES ), "我是测试专用的图片.png" );
@@ -236,22 +237,27 @@ public void onUpdateEnd(Call call) {
236237 return ;
237238 }
238239
240+ /*
239241 // 如果是放到外部存储目录下则需要适配分区存储
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+ */
255261
256262 // 如果是放到外部存储的应用专属目录则不需要适配分区存储特性
257263 File file = new File (getExternalFilesDir (Environment .DIRECTORY_DOWNLOADS ), "微信 8.0.15.apk" );
@@ -262,6 +268,8 @@ public void onUpdateEnd(Call call) {
262268 //.url("https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk")
263269 .url ("https://dldir1.qq.com/weixin/android/weixin8015android2020_arm64.apk" )
264270 .md5 ("b05b25d4738ea31091dd9f80f4416469" )
271+ // 设置断点续传(默认不开启)
272+ .resumableTransfer (true )
265273 .listener (new OnDownloadListener () {
266274
267275 @ Override
@@ -284,10 +292,7 @@ public void onDownloadSuccess(File file) {
284292 @ Override
285293 public void onDownloadFail (File file , Throwable throwable ) {
286294 Toaster .show ("下载失败:" + throwable .getMessage ());
287- if (throwable instanceof FileMd5Exception ) {
288- // 如果是文件 md5 校验失败,则删除文件
289- file .delete ();
290- }
295+ file .delete ();
291296 }
292297
293298 @ Override
0 commit comments