Commit ba03f766fac699c50fb929c45230af8bce046fa5

Authored by 吴奎庆
1 parent 7eb626e7

处理加载慢问题

app/src/main/java/com/cnlive/strike/debug/MainActivity.java
... ... @@ -71,7 +71,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
71 71 .maxImageSize(25 * 1024 * 1024)
72 72 .maxVideoSize(100*1024*1024)
73 73 .isReturnUri(AndroidQUtil.isAndroidQ())
74   - .selectMode(PickerConfig.PICKER_VIDEO)
  74 + .selectMode(PickerConfig.PICKER_IMAGE_VIDEO)
75 75 .defaultSelectList(new ArrayList<Media>())
76 76 .needCamera(true)
77 77 .builder()
... ...
lib_media/src/main/java/com/cnlive/media/frame/view/MediaPickerFragmentView.java
... ... @@ -60,18 +60,18 @@ public class MediaPickerFragmentView implements MvpView {
60 60 //如果可以确定每个item的高度是固定的,设置这个选项可以提高性能
61 61 mFragment.binding.rv.setHasFixedSize(true);
62 62  
63   - mFragment.binding.rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
64   - @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
65   - @Override
66   - public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
67   - super.onScrollStateChanged(recyclerView, newState);
68   - if (RecyclerView.SCROLL_STATE_IDLE == newState) {
69   - Glide.with(recyclerView).resumeRequests();
70   - } else {
71   - Glide.with(recyclerView).pauseRequests();
72   - }
73   - }
74   - });
  63 +// mFragment.binding.rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
  64 +// @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
  65 +// @Override
  66 +// public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  67 +// super.onScrollStateChanged(recyclerView, newState);
  68 +// if (RecyclerView.SCROLL_STATE_IDLE == newState) {
  69 +// Glide.with(recyclerView).resumeRequests();
  70 +// } else {
  71 +// Glide.with(recyclerView).pauseRequests();
  72 +// }
  73 +// }
  74 +// });
75 75 mFragment.mMediaAdapter = new MediaAdapter(mFragment.getActivity(), mActivity.showTime, mActivity.mOptions);
76 76 mFragment.binding.rv.setAdapter(mFragment.mMediaAdapter);
77 77  
... ...
lib_media/src/main/java/com/cnlive/media/utils/GlideCacheUtil.java
... ... @@ -31,8 +31,8 @@ import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
31 31 import com.bumptech.glide.request.RequestListener;
32 32 import com.bumptech.glide.request.RequestOptions;
33 33 import com.bumptech.glide.request.target.Target;
34   -import com.cnlive.media.media.entity.Media;
35 34 import com.cnlive.media.R;
  35 +import com.cnlive.media.media.entity.Media;
36 36  
37 37 import org.reactivestreams.Subscriber;
38 38 import org.reactivestreams.Subscription;
... ... @@ -291,15 +291,11 @@ public class GlideCacheUtil {
291 291 .centerCrop()
292 292 .dontAnimate();
293 293 }
294   -
295 294 if (media.mediaType == 1) {
296   - option = option
297   - .error(R.drawable.iv_imgload_full_err);
  295 + option = option.error(R.drawable.iv_imgload_full_err);
298 296 view.setScaleType(ImageView.ScaleType.CENTER_CROP);
299 297 } else if (media.mediaType == 3) {
300   - option = option
301   - .error(R.drawable.iv_imgload_full_err);
302   -
  298 + option = option.error(R.drawable.iv_imgload_full_err);
303 299 }
304 300  
305 301 if (AndroidQUtil.isAndroidQ()) {
... ... @@ -307,7 +303,6 @@ public class GlideCacheUtil {
307 303 } else {
308 304 rb = rb == null ? rm.load(media.path) : rb.load(media.path);
309 305 }
310   -
311 306 rb.listener(listener).apply(option).into(view);
312 307 } catch (Exception e) {
313 308 }
... ... @@ -328,28 +323,24 @@ public class GlideCacheUtil {
328 323 .dontAnimate();
329 324 }
330 325 if (media.mediaType == 1) {
331   - option = option.error(R.drawable.iv_imgload_full_err);
332   - }else {
333   - option = option.error(R.drawable.iv_imgload_full_err).placeholder(R.drawable.iv_imgload_full_err);
  326 + option = option.error(R.drawable.iv_imgload_full_err).placeholder(R.drawable.iv_new_media_zw);
  327 + } else {
  328 + option = option.error(R.drawable.iv_imgload_full_err).placeholder(R.drawable.iv_new_media_zw);
334 329 }
335   -
336 330 if (AndroidQUtil.isAndroidQ()) {
  331 + view.setTag(mediaUri.toString());
337 332 showAndroidQ(context, media.mediaType, mediaUri, view, loading, option);
338 333 } else {
339 334 if (media.mediaType == 1) {
340   - showNoAndroidQ(context, media.mediaType, media.path, view, loading, option);
341   -// showImageThumbnail(context, media.mediaType, media.id, media.path, view, loading, option);
  335 + showNoAndroidQ(context, media.mediaType, media.fileUri, view, loading, option);
342 336 } else if (media.mediaType == 3) {
343 337 if (ThumbnailsUtils.thumbnails.containsKey(media.id + "")) {
344   - Log.e("包含:",ThumbnailsUtils.thumbnails.get(media.id + ""));
345 338 showVideoNoAndroidQ(context, media.mediaType, ThumbnailsUtils.thumbnails.get(media.id + ""), view, loading, option);
346 339 } else {
  340 + view.setTag(media.id);
347 341 showVideoThumbnail(context, media.mediaType, media.id, media.path, view, loading, option);
348   -// showVideoNoAndroidQ(context, media.mediaType, media.path, view, loading, option);
349 342 }
350   -//
351 343 }
352   -
353 344 }
354 345  
355 346  
... ... @@ -358,50 +349,57 @@ public class GlideCacheUtil {
358 349 }
359 350 }
360 351  
  352 + private static void showAndroidQUri(Context context, Uri path, ImageView view, ProgressBar loading, RequestOptions option) {
  353 + RequestBuilder rb = Glide.with(context).load(path);
  354 + rb.listener(new RequestListener() {
  355 + @Override
  356 + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
  357 + view.setScaleType(ImageView.ScaleType.CENTER);
  358 + if (loading != null) loading.setVisibility(View.GONE);
  359 + return false;
  360 + }
  361 +
  362 + @Override
  363 + public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
  364 + if (loading != null) loading.setVisibility(View.GONE);
  365 + return false;
  366 + }
  367 + });
  368 +
  369 + rb.apply(option).into(view);
  370 + }
  371 +
361 372 private static void showAndroidQ(Context context, int mediaType, Uri mediaUri, ImageView view, ProgressBar loading, RequestOptions option) {
  373 +
362 374 Observable.create(new ObservableOnSubscribe<Bitmap>() {
363 375 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
364 376 @Override
365 377 public void subscribe(ObservableEmitter<Bitmap> emitter) throws Exception {
366 378 ContentResolver contentResolver = context.getContentResolver();
367 379 Bitmap bm = contentResolver.loadThumbnail(mediaUri, new Size(100, 100), null);
368   - if (bm!=null){
  380 + if (bm != null) {
369 381 emitter.onNext(bm);
370   - }else {
  382 + } else {
371 383 emitter.onError(new Throwable(""));
372 384 }
373   -
374 385 }
375 386 }).subscribeOn(Schedulers.computation()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Bitmap>() {
376 387 @Override
377 388 public void onSubscribe(Disposable d) {
378   -
379 389 }
380 390  
381 391 @Override
382   - public void onNext(Bitmap bm) {
383   -
384   - RequestBuilder rb = Glide.with(context).load(bm);
385   - rb.listener(new RequestListener() {
386   - @Override
387   - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
388   - view.setScaleType(ImageView.ScaleType.CENTER);
389   - if (loading != null) loading.setVisibility(View.GONE);
390   - return false;
391   - }
392   -
393   - @Override
394   - public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
395   - if (loading != null) loading.setVisibility(View.GONE);
396   - return false;
397   - }
398   - });
399   -
400   - rb.apply(option).into(view);
  392 + public void onNext(Bitmap bean) {
  393 + if (view.getTag() != null && mediaUri.toString().equals(view.getTag())) {
  394 + showThumbnail(context, bean, view, loading, option);
  395 + }
401 396 }
402 397  
403 398 @Override
404 399 public void onError(Throwable e) {
  400 + if (view.getTag() != null && mediaUri.toString().equals(view.getTag())) {
  401 + showAndroidQUri(context, mediaUri, view, loading, option);
  402 + }
405 403 if (loading != null) loading.setVisibility(View.GONE);
406 404 }
407 405  
... ... @@ -452,11 +450,8 @@ public class GlideCacheUtil {
452 450 rb.thumbnail(0.3f).apply(option).into(view);
453 451 }
454 452  
455   - public static int count = 0;
456 453  
457 454 private static void showVideoThumbnail(Context context, int mediaType, long id, String path, ImageView view, ProgressBar loading, RequestOptions option) {
458   -// Bitmap bm = getVideoThumbnail(context, id + "");
459   -// showThumbnail(context, bm, view, loading, option);
460 455 // // 步骤1:创建被观察者 = Flowable
461 456 Flowable.create(new FlowableOnSubscribe<Bitmap>() {
462 457 @Override
... ... @@ -467,21 +462,17 @@ public class GlideCacheUtil {
467 462 } else {
468 463 emitter.onError(new Throwable(""));
469 464 }
470   -
471   -// emitter.onComplete();
472 465 }
473 466 }, BackpressureStrategy.LATEST)
474 467 .subscribe(new Subscriber<Bitmap>() {
475   - // 步骤2:创建观察者 = Subscriber & 建立订阅关系
476   -
477 468 @Override
478 469 public void onSubscribe(Subscription s) {
479   -// s.request(150);
480 470 }
481 471  
482 472 @Override
483 473 public void onNext(Bitmap thumbnails) {
484   - showThumbnail(context, thumbnails, view, loading, option);
  474 + if (view.getTag() != null && view.getTag().toString().equals(id + ""))
  475 + showThumbnail(context, thumbnails, view, loading, option);
485 476 }
486 477  
487 478 @Override
... ... @@ -494,100 +485,9 @@ public class GlideCacheUtil {
494 485 public void onComplete() {
495 486 }
496 487 });
497   -
498   -
499   - Observable.create(new ObservableOnSubscribe<Bitmap>() {
500   - @Override
501   - public void subscribe(ObservableEmitter<Bitmap> emitter) throws Exception {
502   - count += 1;
503   - Log.e("显示测试:", count + "");
504   - Bitmap bitmap = getVideoThumbnail(context, id + "");
505   - emitter.onNext(bitmap);
506   -//
507   -// Cursor thumbCursor = null;
508   -// Bitmap bitmap = null;
509   -// thumbCursor = context.getContentResolver().query(
510   -// MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
511   -// null, MediaStore.Video.Thumbnails.VIDEO_ID
512   -// + "=" + id, null, null);
513   -// if (thumbCursor.moveToFirst()) {
514   -// String albumPath = thumbCursor.getString(thumbCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
515   -// Log.e("显示测试地址:", albumPath);
516   -// bitmap = BitmapFactory.decodeFile(albumPath);
517   -// emitter.onNext(bitmap);
518   -// }else {
519   -// emitter.onNext(Bitmap.createBitmap(1,1,Bitmap.Config.RGB_565));
520   -// }
521   -
522   -
523   - }
524   - }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Bitmap>() {
525   - @Override
526   - public void onSubscribe(Disposable d) {
527   - Log.e("显示测试:--->", "onSubscribe:" + count + "");
528   - }
529   -
530   - @Override
531   - public void onNext(Bitmap thumbnails) {
532   - Log.e("显示测试:--->", "onNext:" + count + "");
533   - showThumbnail(context, thumbnails, view, loading, option);
534   -
535   - }
536   -
537   - @Override
538   - public void onError(Throwable e) {
539   - showNoAndroidQ(context, mediaType, path, view, loading, option);
540   - if (loading != null) loading.setVisibility(View.GONE);
541   - }
542   -
543   - @Override
544   - public void onComplete() {
545   - Log.e("显示测试:--->", "onComplete:" + count + "");
546   - }
547   - });
548 488 }
549 489  
550 490  
551   - private static void showImageThumbnail(Context context, int mediaType, int id, String path, ImageView view, ProgressBar loading, RequestOptions option) {
552   - Observable.create(new ObservableOnSubscribe<Bitmap>() {
553   - @Override
554   - public void subscribe(ObservableEmitter<Bitmap> emitter) throws Exception {
555   - Bitmap bm = null;
556   - if (mediaType == 1) {
557   - bm = getImageThumbnail(context, id + "");
558   - emitter.onNext(bm);
559   - }
560   -
561   -
562   - }
563   - }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Bitmap>() {
564   - @Override
565   - public void onSubscribe(Disposable d) {
566   -
567   - }
568   -
569   - @Override
570   - public void onNext(Bitmap thumbnails) {
571   - if (thumbnails != null) {
572   - showThumbnail(context, thumbnails, view, loading, option);
573   - } else {
574   - showNoAndroidQ(context, mediaType, path, view, loading, option);
575   - }
576   -
577   - }
578   -
579   - @Override
580   - public void onError(Throwable e) {
581   - if (loading != null) loading.setVisibility(View.GONE);
582   - }
583   -
584   - @Override
585   - public void onComplete() {
586   -
587   - }
588   - });
589   - }
590   -
591 491 private static void showThumbnail(Context context, Bitmap bm, ImageView view, ProgressBar loading, RequestOptions option) {
592 492 RequestBuilder rb = Glide.with(context).load(bm);
593 493 rb.listener(new RequestListener() {
... ... @@ -619,7 +519,7 @@ public class GlideCacheUtil {
619 519 + "=" + videoId, null, null);
620 520 if (thumbCursor.moveToFirst()) {
621 521 String albumPath = thumbCursor.getString(thumbCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
622   - Log.d("缩略图----------------->:","id:"+videoId+"地址:"+ albumPath);
  522 + Log.d("缩略图----------------->:", "id:" + videoId + "地址:" + albumPath);
623 523 bitmap = BitmapFactory.decodeFile(albumPath);
624 524 return bitmap;
625 525 }
... ... @@ -635,14 +535,6 @@ public class GlideCacheUtil {
635 535 public static Bitmap getImageThumbnail(Context context, String videoId) {
636 536 Bitmap bitmap = null;
637 537 Cursor thumbCursor = null;
638   -
639   -
640   -// String[] projection = {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID,
641   -// MediaStore.Images.Thumbnails.DATA};
642   -//
643   -// Cursor cursor = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null);
644   -//
645   -
646 538 try {
647 539 thumbCursor = context.getApplicationContext().getContentResolver().query(
648 540 MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
... ...
lib_media/src/main/java/com/cnlive/media/utils/ThumbnailsUtils.java
... ... @@ -36,10 +36,31 @@ public class ThumbnailsUtils {
36 36 _id = cursor.getInt(_idColumn);
37 37 image_id = cursor.getInt(image_idColumn);
38 38 image_path = cursor.getString(dataColumn);
  39 + Log.e("缩略图视频:","--->videoId:"+image_id+" videoPath:"+image_path);
39 40 thumbnails.put(image_id + "", image_path);
40 41 }
  42 + getImageThumbnails(context);
  43 + }
41 44  
  45 + public static void getImageThumbnails(Context context) {
  46 + String[] projection = {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID,
  47 + MediaStore.Images.Thumbnails.DATA};
42 48  
  49 + Cursor cursor = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null);
  50 + if (cursor == null) return;
  51 + while (cursor.moveToNext()) {
  52 + int _id;
  53 + int image_id;
  54 + String image_path;
  55 + int _idColumn = cursor.getColumnIndex(MediaStore.Images.Thumbnails._ID);
  56 + int image_idColumn = cursor.getColumnIndex(MediaStore.Images.Thumbnails.IMAGE_ID);
  57 + int dataColumn = cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA);
  58 + _id = cursor.getInt(_idColumn);
  59 + image_id = cursor.getInt(image_idColumn);
  60 + image_path = cursor.getString(dataColumn);
  61 + Log.e("缩略图图片:","--->image_id:"+image_id+" image_path:"+image_path);
  62 + thumbnails.put(image_id + "", image_path);
  63 + }
43 64 }
44 65  
45 66  
... ...
lib_media/src/main/java/com/cnlive/media/utils/glide/DataCacheKey.java 0 → 100644
  1 +package com.cnlive.media.utils.glide;
  2 +
  3 +/**
  4 + * 作 者 : wkq
  5 + * <p>
  6 + * 时 间 : 2021/2/1 11:05
  7 + * <p>
  8 + * 名 字 : DataCacheKey
  9 + * <p>
  10 + * 简 介 :
  11 + */
  12 +
  13 +import com.bumptech.glide.load.Key;
  14 +
  15 +import java.security.MessageDigest;
  16 +
  17 +/**
  18 + * A cache key for original source data + any requested signature.
  19 + */
  20 +public class DataCacheKey implements Key {
  21 +
  22 + private final Key sourceKey;
  23 + private final Key signature;
  24 +
  25 + public DataCacheKey(Key sourceKey, Key signature) {
  26 + this.sourceKey = sourceKey;
  27 + this.signature = signature;
  28 + }
  29 +
  30 + public Key getSourceKey() {
  31 + return sourceKey;
  32 + }
  33 +
  34 + @Override
  35 + public boolean equals(Object o) {
  36 + if (o instanceof DataCacheKey) {
  37 + DataCacheKey other = (DataCacheKey) o;
  38 + return sourceKey.equals(other.sourceKey) && signature.equals(other.signature);
  39 + }
  40 + return false;
  41 + }
  42 +
  43 + @Override
  44 + public int hashCode() {
  45 + int result = sourceKey.hashCode();
  46 + result = 31 * result + signature.hashCode();
  47 + return result;
  48 + }
  49 +
  50 + @Override
  51 + public String toString() {
  52 + return "DataCacheKey{"
  53 + + "sourceKey=" + sourceKey
  54 + + ", signature=" + signature
  55 + + '}';
  56 + }
  57 +
  58 + @Override
  59 + public void updateDiskCacheKey(MessageDigest messageDigest) {
  60 + sourceKey.updateDiskCacheKey(messageDigest);
  61 + signature.updateDiskCacheKey(messageDigest);
  62 + }
  63 +}
... ...
lib_media/src/main/res/drawable-xhdpi/iv_new_media_zw.png 0 → 100644

5.23 KB