Commit 23cbe5defb989cace463b2b26710bdf4166bebda
1 parent
be88f8c1
处理展示效率问题
Showing
13 changed files
with
378 additions
and
57 deletions
app/src/main/java/com/cnlive/strike/debug/MainActivity.java
| ... | ... | @@ -66,7 +66,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe |
| 66 | 66 | case R.id.tv_media: |
| 67 | 67 | //打开相册 |
| 68 | 68 | new ImagePicker.Builder() |
| 69 | - .maxNum(1) | |
| 69 | + .maxNum(9) | |
| 70 | 70 | .setSelectGif(true) |
| 71 | 71 | .maxImageSize(25 * 1024 * 1024) |
| 72 | 72 | .maxVideoSize(100*1024*1024) | ... | ... |
lib_media/src/main/java/com/cnlive/media/PickerConfig.java
| ... | ... | @@ -48,7 +48,7 @@ public class PickerConfig { |
| 48 | 48 | //视频选一个 图片选九个 |
| 49 | 49 | public static final int PICKER_ONE_VIDEO_TYPE = 103; |
| 50 | 50 | //展示图片的列数行数 |
| 51 | - public static int GridSpanCount = 3; | |
| 51 | + public static int GridSpanCount = 4; | |
| 52 | 52 | public static int GridSpace = 4; |
| 53 | 53 | |
| 54 | 54 | //裁剪请求码 | ... | ... |
lib_media/src/main/java/com/cnlive/media/frame/view/MediaPickerFragmentView.java
| ... | ... | @@ -2,6 +2,7 @@ package com.cnlive.media.frame.view; |
| 2 | 2 | |
| 3 | 3 | import android.os.Build; |
| 4 | 4 | import android.text.TextUtils; |
| 5 | +import android.util.Log; | |
| 5 | 6 | |
| 6 | 7 | import androidx.annotation.RequiresApi; |
| 7 | 8 | import androidx.recyclerview.widget.GridLayoutManager; |
| ... | ... | @@ -61,18 +62,20 @@ public class MediaPickerFragmentView implements MvpView { |
| 61 | 62 | //如果可以确定每个item的高度是固定的,设置这个选项可以提高性能 |
| 62 | 63 | mFragment.binding.rv.setHasFixedSize(true); |
| 63 | 64 | |
| 64 | - mFragment.binding.rv.addOnScrollListener(new RecyclerView.OnScrollListener() { | |
| 65 | - @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) | |
| 66 | - @Override | |
| 67 | - public void onScrollStateChanged(RecyclerView recyclerView, int newState) { | |
| 68 | - super.onScrollStateChanged(recyclerView, newState); | |
| 69 | - if (RecyclerView.SCROLL_STATE_IDLE == newState) { | |
| 70 | - Glide.with(recyclerView).resumeRequests(); | |
| 71 | - } else { | |
| 72 | - Glide.with(recyclerView).pauseRequests(); | |
| 73 | - } | |
| 74 | - } | |
| 75 | - }); | |
| 65 | +// mFragment.binding.rv.addOnScrollListener(new RecyclerView.OnScrollListener() { | |
| 66 | +// @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) | |
| 67 | +// @Override | |
| 68 | +// public void onScrollStateChanged(RecyclerView recyclerView, int newState) { | |
| 69 | +// super.onScrollStateChanged(recyclerView, newState); | |
| 70 | +// if (RecyclerView.SCROLL_STATE_IDLE == newState) { | |
| 71 | +// Glide.with(recyclerView).resumeRequests(); | |
| 72 | +// Log.e("",""); | |
| 73 | +// } else { | |
| 74 | +// Log.e("",""); | |
| 75 | +// Glide.with(recyclerView).pauseRequests(); | |
| 76 | +// } | |
| 77 | +// } | |
| 78 | +// }); | |
| 76 | 79 | mFragment.mMediaAdapter = new MediaAdapter(mFragment.getActivity(), mActivity.showTime, mActivity.mOptions); |
| 77 | 80 | mFragment.binding.rv.setAdapter(mFragment.mMediaAdapter); |
| 78 | 81 | ... | ... |
lib_media/src/main/java/com/cnlive/media/media/entity/Media.java
lib_media/src/main/java/com/cnlive/media/media/loader/ImageLoader.java
| ... | ... | @@ -128,7 +128,8 @@ public class ImageLoader extends LoaderM implements LoaderManager.LoaderCallback |
| 128 | 128 | media.setImgHeight(height); |
| 129 | 129 | media.setLongImg(MediaUtils.isLongImg(media)); |
| 130 | 130 | allFolder.addMedias(media); |
| 131 | - | |
| 131 | +// String thumbnails = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA)); | |
| 132 | +// media.thumbnails=thumbnails; | |
| 132 | 133 | int index = hasDir(folders, dirName); |
| 133 | 134 | if (index != -1) { |
| 134 | 135 | folders.get(index).addMedias(media); | ... | ... |
lib_media/src/main/java/com/cnlive/media/media/loader/MediaLoader.java
| 1 | 1 | package com.cnlive.media.media.loader; |
| 2 | 2 | |
| 3 | 3 | import android.app.LoaderManager; |
| 4 | +import android.content.ContentResolver; | |
| 4 | 5 | import android.content.Context; |
| 5 | 6 | import android.content.CursorLoader; |
| 6 | 7 | import android.content.Loader; |
| 7 | 8 | import android.database.Cursor; |
| 9 | +import android.graphics.Bitmap; | |
| 8 | 10 | import android.net.Uri; |
| 9 | 11 | import android.os.Bundle; |
| 10 | 12 | import android.provider.MediaStore; |
| 11 | 13 | import android.text.TextUtils; |
| 14 | +import android.util.Log; | |
| 15 | +import android.util.Size; | |
| 12 | 16 | |
| 13 | 17 | import com.cnlive.media.media.DataCallback; |
| 14 | 18 | import com.cnlive.media.media.entity.Folder; |
| 15 | 19 | import com.cnlive.media.media.entity.Media; |
| 20 | +import com.cnlive.media.utils.AndroidQUtil; | |
| 16 | 21 | import com.cnlive.media.utils.MediaUtils; |
| 17 | 22 | import com.cnlive.media.R; |
| 18 | 23 | |
| ... | ... | @@ -71,7 +76,9 @@ public class MediaLoader extends LoaderM implements LoaderManager.LoaderCallback |
| 71 | 76 | + MediaStore.Files.FileColumns.MEDIA_TYPE + "=" |
| 72 | 77 | + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO; |
| 73 | 78 | |
| 79 | + | |
| 74 | 80 | Uri queryUri = MediaStore.Files.getContentUri("external"); |
| 81 | + | |
| 75 | 82 | CursorLoader cursorLoader = new CursorLoader( |
| 76 | 83 | mContext, |
| 77 | 84 | queryUri, |
| ... | ... | @@ -90,6 +97,7 @@ public class MediaLoader extends LoaderM implements LoaderManager.LoaderCallback |
| 90 | 97 | mLoader.onData(folders); |
| 91 | 98 | return; |
| 92 | 99 | } |
| 100 | + | |
| 93 | 101 | Folder allFolder = new Folder(mContext.getResources().getString(R.string.all_dir_name)); |
| 94 | 102 | folders.add(allFolder); |
| 95 | 103 | Folder allVideoDir = new Folder(mContext.getResources().getString(R.string.video_dir_name)); |
| ... | ... | @@ -131,6 +139,12 @@ public class MediaLoader extends LoaderM implements LoaderManager.LoaderCallback |
| 131 | 139 | allFolder.addMedias(media); |
| 132 | 140 | if (mediaType == 3) { |
| 133 | 141 | allVideoDir.addMedias(media); |
| 142 | +// String thumbnails = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA)); | |
| 143 | +// ContentResolver contentResolver = mContext.getContentResolver(); | |
| 144 | +// media.thumbnails = thumbnails; | |
| 145 | + } else if (mediaType == 1) { | |
| 146 | +// String thumbnails = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA)); | |
| 147 | +// media.thumbnails = thumbnails; | |
| 134 | 148 | } |
| 135 | 149 | |
| 136 | 150 | int index = hasDir(folders, dirName); | ... | ... |
lib_media/src/main/java/com/cnlive/media/media/loader/VideoLoader.java
| ... | ... | @@ -87,7 +87,6 @@ public class VideoLoader extends LoaderM implements LoaderManager.LoaderCallback |
| 87 | 87 | Folder allFolder = new Folder(mContext.getResources().getString(R.string.all_video)); |
| 88 | 88 | folders.add(allFolder); |
| 89 | 89 | Cursor cursor = (Cursor) o; |
| 90 | - Log.e("dmc", cursor.getCount() + "数量数量"); | |
| 91 | 90 | while (cursor.moveToNext()) { |
| 92 | 91 | //获取的视频路径 |
| 93 | 92 | String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA)); |
| ... | ... | @@ -112,6 +111,8 @@ public class VideoLoader extends LoaderM implements LoaderManager.LoaderCallback |
| 112 | 111 | String dirName = getParent(path); |
| 113 | 112 | if (TextUtils.isEmpty(dirName)) continue; |
| 114 | 113 | Media media = new Media(path, name, dateTime, mediaType, size, id, dirName, duration, fileUri); |
| 114 | +// String thumbnails = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA)); | |
| 115 | +// media.thumbnails=thumbnails; | |
| 115 | 116 | allFolder.addMedias(media); |
| 116 | 117 | |
| 117 | 118 | int index = hasDir(folders, dirName); | ... | ... |
lib_media/src/main/java/com/cnlive/media/ui/activity/MediaActivity.java
| ... | ... | @@ -28,6 +28,7 @@ import com.cnlive.media.model.ImagePickerOptions; |
| 28 | 28 | import com.cnlive.media.ui.adapter.FolderAdapter; |
| 29 | 29 | import com.cnlive.media.ui.fragment.MediaPickerFragment; |
| 30 | 30 | import com.cnlive.media.ui.fragment.MediaPreviewFragment; |
| 31 | +import com.cnlive.media.utils.ThumbnailsUtils; | |
| 31 | 32 | import com.cnlive.media.utils.observable.MediaAddObservable; |
| 32 | 33 | import com.cnlive.media.utils.observable.MediaOpenActivityObservable; |
| 33 | 34 | import com.cnlive.media.utils.observable.MediaOpenInfo; |
| ... | ... | @@ -140,11 +141,11 @@ public class MediaActivity extends MvpBindingActivity<MediaView, MediaPresenter, |
| 140 | 141 | protected void onCreate(Bundle savedInstanceState) { |
| 141 | 142 | super.onCreate(savedInstanceState); |
| 142 | 143 | //初始化携带数据 |
| 144 | + | |
| 143 | 145 | if (getPresenter() != null) getPresenter().initIntentData(this); |
| 144 | 146 | if (getMvpView() != null) getMvpView().initView(); |
| 145 | 147 | getSupportFragmentManager().beginTransaction().add(R.id.frame, MediaPickerFragment.newInstanse(), MEDIA_HOME).addToBackStack("").commit(); |
| 146 | 148 | getMediaData(); |
| 147 | - | |
| 148 | 149 | } |
| 149 | 150 | |
| 150 | 151 | void getMediaData() { |
| ... | ... | @@ -152,6 +153,7 @@ public class MediaActivity extends MvpBindingActivity<MediaView, MediaPresenter, |
| 152 | 153 | .checkPermissions(this, permissionsRead, REQUEST_CODE_PERMISSION_READ, getResources().getString(R.string.dialog_imagepicker_permission_sdcard_nerver_ask_message), true); |
| 153 | 154 | if (hasPermissions) { |
| 154 | 155 | if (needCrop && singlePick) { |
| 156 | +// ThumbnailsUtils.getThumbnails(this); | |
| 155 | 157 | selectMode = PickerConfig.PICKER_IMAGE; |
| 156 | 158 | } |
| 157 | 159 | int type = selectMode; | ... | ... |
lib_media/src/main/java/com/cnlive/media/ui/adapter/MediaAdapter.java
| 1 | 1 | package com.cnlive.media.ui.adapter; |
| 2 | 2 | |
| 3 | +import android.content.ContentResolver; | |
| 3 | 4 | import android.content.Context; |
| 4 | 5 | import android.view.LayoutInflater; |
| 5 | 6 | import android.view.View; |
| ... | ... | @@ -118,6 +119,9 @@ public class MediaAdapter extends BaseRecyclerViewAdapter<Media> { |
| 118 | 119 | private void showMedia(MediaViewHolder mediaViewHolder, int position) { |
| 119 | 120 | Media mMedia = getItem(position); |
| 120 | 121 | binding = (MediaImageItemBinding) mediaViewHolder.getBinding(); |
| 122 | + | |
| 123 | +// ContentResolver resolver = mContxt.getContentResolver(); | |
| 124 | +// resolver.loadThumbnail() | |
| 121 | 125 | GlideCacheUtil.intoItemImageThumbnail(mContext, mMedia, binding.mediaImage, null); |
| 122 | 126 | if (mMedia.mediaType == 1) { |
| 123 | 127 | binding.videoInfo.setVisibility(View.GONE); | ... | ... |
lib_media/src/main/java/com/cnlive/media/ui/adapter/PreviewBottomAdapter.java
| ... | ... | @@ -52,7 +52,7 @@ public class PreviewBottomAdapter extends RecyclerView.Adapter<PreviewBottomAdap |
| 52 | 52 | @Override |
| 53 | 53 | public void onBindViewHolder(final ViewHolder holder, final int position) { |
| 54 | 54 | //填充图片 |
| 55 | - GlideCacheUtil.intoItemImage(context, mItems.get(position), holder.rv_item_image, null); | |
| 55 | + GlideCacheUtil.intoItemImageThumbnail(context, mItems.get(position), holder.rv_item_image, null); | |
| 56 | 56 | |
| 57 | 57 | if (selectedPosition == position) { |
| 58 | 58 | holder.rv_item_image.setBackgroundResource(R.drawable.shape_green_square_bg); | ... | ... |
lib_media/src/main/java/com/cnlive/media/ui/adapter/PreviewBottomMediaAdapter.java
| ... | ... | @@ -53,7 +53,7 @@ public class PreviewBottomMediaAdapter extends BaseRecyclerViewAdapter<Media> { |
| 53 | 53 | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { |
| 54 | 54 | MediaViewHolder mediaViewHolder = (MediaViewHolder) holder; |
| 55 | 55 | MediaPreviewBottomBarItemBinding bottomBarItemBinding = (MediaPreviewBottomBarItemBinding) mediaViewHolder.getBinding(); |
| 56 | - GlideCacheUtil.intoItemImage(context, getItem(position), bottomBarItemBinding.bottomItemImageView, null); | |
| 56 | + GlideCacheUtil.intoItemImageThumbnail(context, getItem(position), bottomBarItemBinding.bottomItemImageView, null); | |
| 57 | 57 | if (isPre) { |
| 58 | 58 | if (selectedPosition == position) { |
| 59 | 59 | bottomBarItemBinding.bottomItemImageView.setBackgroundResource(R.drawable.shape_green_square_bg); | ... | ... |
lib_media/src/main/java/com/cnlive/media/utils/GlideCacheUtil.java
| 1 | 1 | package com.cnlive.media.utils; |
| 2 | 2 | |
| 3 | 3 | import android.annotation.SuppressLint; |
| 4 | +import android.content.ContentResolver; | |
| 4 | 5 | import android.content.Context; |
| 6 | +import android.database.Cursor; | |
| 5 | 7 | import android.graphics.Bitmap; |
| 8 | +import android.graphics.BitmapFactory; | |
| 6 | 9 | import android.net.Uri; |
| 10 | +import android.os.Build; | |
| 7 | 11 | import android.os.Looper; |
| 12 | +import android.provider.MediaStore; | |
| 8 | 13 | import android.text.TextUtils; |
| 9 | 14 | import android.util.Log; |
| 15 | +import android.util.Size; | |
| 10 | 16 | import android.view.View; |
| 11 | 17 | import android.widget.ImageView; |
| 12 | 18 | import android.widget.ProgressBar; |
| 13 | 19 | |
| 14 | 20 | import androidx.annotation.Nullable; |
| 21 | +import androidx.annotation.RequiresApi; | |
| 15 | 22 | |
| 16 | 23 | import com.bumptech.glide.Glide; |
| 17 | 24 | import com.bumptech.glide.RequestBuilder; |
| ... | ... | @@ -30,6 +37,14 @@ import com.cnlive.media.R; |
| 30 | 37 | import java.io.File; |
| 31 | 38 | import java.math.BigDecimal; |
| 32 | 39 | |
| 40 | +import io.reactivex.Observable; | |
| 41 | +import io.reactivex.ObservableEmitter; | |
| 42 | +import io.reactivex.ObservableOnSubscribe; | |
| 43 | +import io.reactivex.Observer; | |
| 44 | +import io.reactivex.android.schedulers.AndroidSchedulers; | |
| 45 | +import io.reactivex.disposables.Disposable; | |
| 46 | +import io.reactivex.schedulers.Schedulers; | |
| 47 | + | |
| 33 | 48 | /** |
| 34 | 49 | * Created by Lynn on 2018/1/19. |
| 35 | 50 | */ |
| ... | ... | @@ -216,7 +231,7 @@ public class GlideCacheUtil { |
| 216 | 231 | if (media.mediaType == 1) { |
| 217 | 232 | option = option |
| 218 | 233 | .error(R.drawable.iv_imgload_full_err); |
| 219 | - }else if (media.mediaType == 1){ | |
| 234 | + } else if (media.mediaType == 1) { | |
| 220 | 235 | option = option |
| 221 | 236 | .error(R.drawable.iv_imgload_full_err); |
| 222 | 237 | view.setScaleType(ImageView.ScaleType.CENTER_CROP); |
| ... | ... | @@ -250,9 +265,8 @@ public class GlideCacheUtil { |
| 250 | 265 | } |
| 251 | 266 | |
| 252 | 267 | |
| 253 | - | |
| 254 | 268 | @SuppressLint("CheckResult") |
| 255 | - public static void intoItemImageListener(Context context, Media media, ImageView view, RequestListener listener) { | |
| 269 | + public static void intoItemImageListener(Context context, Media media, ImageView view, RequestListener listener) { | |
| 256 | 270 | if (context == null || media == null || view == null) return; |
| 257 | 271 | |
| 258 | 272 | RequestOptions option = new RequestOptions(); |
| ... | ... | @@ -275,7 +289,7 @@ public class GlideCacheUtil { |
| 275 | 289 | option = option |
| 276 | 290 | .error(R.drawable.iv_imgload_full_err); |
| 277 | 291 | view.setScaleType(ImageView.ScaleType.CENTER_CROP); |
| 278 | - }else if (media.mediaType == 3){ | |
| 292 | + } else if (media.mediaType == 3) { | |
| 279 | 293 | option = option |
| 280 | 294 | .error(R.drawable.iv_imgload_full_err); |
| 281 | 295 | |
| ... | ... | @@ -287,68 +301,302 @@ public class GlideCacheUtil { |
| 287 | 301 | rb = rb == null ? rm.load(media.path) : rb.load(media.path); |
| 288 | 302 | } |
| 289 | 303 | |
| 290 | - rb.listener( listener).apply(option).into(view); | |
| 304 | + rb.listener(listener).apply(option).into(view); | |
| 291 | 305 | } catch (Exception e) { |
| 292 | 306 | } |
| 293 | 307 | } |
| 294 | 308 | |
| 295 | - static long start=0; | |
| 296 | 309 | |
| 297 | 310 | @SuppressLint("CheckResult") |
| 298 | 311 | public static void intoItemImageThumbnail(Context context, Media media, ImageView view, ProgressBar loading) { |
| 299 | 312 | if (context == null || media == null || view == null) return; |
| 300 | - | |
| 301 | 313 | RequestOptions option = new RequestOptions(); |
| 302 | - | |
| 303 | 314 | try { |
| 304 | 315 | Uri mediaUri = Uri.parse(media.fileUri); |
| 305 | - | |
| 306 | - RequestManager rm = Glide.with(context); | |
| 307 | - RequestBuilder rb = null; | |
| 308 | - | |
| 309 | 316 | view.setScaleType(ImageView.ScaleType.CENTER_CROP); |
| 310 | - | |
| 311 | 317 | if (FileTypeUtil.isGif(media.path)) { |
| 312 | -// rb = rm.asGif(); | |
| 313 | 318 | } else { |
| 314 | 319 | option = option |
| 315 | - .diskCacheStrategy(DiskCacheStrategy.RESOURCE) | |
| 320 | + .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC) | |
| 316 | 321 | .dontAnimate(); |
| 317 | 322 | } |
| 318 | - | |
| 319 | 323 | if (media.mediaType == 1) { |
| 320 | - option = option | |
| 321 | - .error(R.drawable.iv_imgload_full_err); | |
| 324 | + option = option.error(R.drawable.iv_imgload_full_err); | |
| 322 | 325 | } |
| 323 | - | |
| 324 | 326 | if (AndroidQUtil.isAndroidQ()) { |
| 325 | - rb = rb == null ? rm.load(mediaUri) : rb.load(mediaUri); | |
| 327 | + showAndroidQ(context, media.mediaType, mediaUri, view, loading, option); | |
| 326 | 328 | } else { |
| 327 | - rb = rb == null ? rm.load(media.path) : rb.load(media.path); | |
| 329 | + if (media.mediaType == 1) { | |
| 330 | + showNoAndroidQ(context, media.mediaType, media.path, view, loading, option); | |
| 331 | +// showImageThumbnail(context, media.mediaType, media.id, media.path, view, loading, option); | |
| 332 | + } else if (media.mediaType == 3) { | |
| 333 | +// showNoAndroidQ(context, media.mediaType, media.path, view, loading, option); | |
| 334 | + showVideoThumbnail(context, media.mediaType, media.id, media.path, view, loading, option); | |
| 335 | + } | |
| 336 | + | |
| 328 | 337 | } |
| 329 | - rb.thumbnail(0.5f); | |
| 330 | 338 | |
| 331 | - rb.listener(new RequestListener() { | |
| 332 | - @Override | |
| 333 | - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { | |
| 334 | - view.setScaleType(ImageView.ScaleType.CENTER); | |
| 335 | - if (loading != null) loading.setVisibility(View.GONE); | |
| 336 | - return false; | |
| 339 | + | |
| 340 | + } catch (Exception e) { | |
| 341 | + if (loading != null) loading.setVisibility(View.GONE); | |
| 342 | + } | |
| 343 | + } | |
| 344 | + | |
| 345 | + private static void showAndroidQ(Context context, int mediaType, Uri mediaUri, ImageView view, ProgressBar loading, RequestOptions option) { | |
| 346 | + Observable.create(new ObservableOnSubscribe<Bitmap>() { | |
| 347 | + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
| 348 | + @Override | |
| 349 | + public void subscribe(ObservableEmitter<Bitmap> emitter) throws Exception { | |
| 350 | + ContentResolver contentResolver = context.getContentResolver(); | |
| 351 | + Bitmap bm = contentResolver.loadThumbnail(mediaUri, new Size(100, 100), null); | |
| 352 | + emitter.onNext(bm); | |
| 353 | + } | |
| 354 | + }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Bitmap>() { | |
| 355 | + @Override | |
| 356 | + public void onSubscribe(Disposable d) { | |
| 357 | + | |
| 358 | + } | |
| 359 | + | |
| 360 | + @Override | |
| 361 | + public void onNext(Bitmap bm) { | |
| 362 | + | |
| 363 | + RequestBuilder rb = Glide.with(context).load(bm); | |
| 364 | + rb.listener(new RequestListener() { | |
| 365 | + @Override | |
| 366 | + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { | |
| 367 | + view.setScaleType(ImageView.ScaleType.CENTER); | |
| 368 | + if (loading != null) loading.setVisibility(View.GONE); | |
| 369 | + return false; | |
| 370 | + } | |
| 371 | + | |
| 372 | + @Override | |
| 373 | + public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { | |
| 374 | + if (loading != null) loading.setVisibility(View.GONE); | |
| 375 | + return false; | |
| 376 | + } | |
| 377 | + }); | |
| 378 | + | |
| 379 | + rb.apply(option).into(view); | |
| 380 | + } | |
| 381 | + | |
| 382 | + @Override | |
| 383 | + public void onError(Throwable e) { | |
| 384 | + if (loading != null) loading.setVisibility(View.GONE); | |
| 385 | + } | |
| 386 | + | |
| 387 | + @Override | |
| 388 | + public void onComplete() { | |
| 389 | + | |
| 390 | + } | |
| 391 | + }); | |
| 392 | + } | |
| 393 | + | |
| 394 | + | |
| 395 | + private static void showNoAndroidQ(Context context, int mediaType, String path, ImageView view, ProgressBar loading, RequestOptions option) { | |
| 396 | + Observable.create(new ObservableOnSubscribe<String>() { | |
| 397 | + @Override | |
| 398 | + public void subscribe(ObservableEmitter<String> emitter) throws Exception { | |
| 399 | + | |
| 400 | + if (TextUtils.isEmpty(path)) { | |
| 401 | + emitter.onError(new Throwable("")); | |
| 402 | + } else { | |
| 403 | + emitter.onNext(path); | |
| 337 | 404 | } |
| 338 | 405 | |
| 339 | - @Override | |
| 340 | - public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { | |
| 341 | - if (loading != null) loading.setVisibility(View.GONE); | |
| 406 | + } | |
| 407 | + }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() { | |
| 408 | + @Override | |
| 409 | + public void onSubscribe(Disposable d) { | |
| 342 | 410 | |
| 343 | - Log.e("加载:",System.currentTimeMillis()-start+""); | |
| 344 | - return false; | |
| 411 | + } | |
| 412 | + | |
| 413 | + @Override | |
| 414 | + public void onNext(String thumbnails) { | |
| 415 | + RequestBuilder rb = Glide.with(context).load(thumbnails); | |
| 416 | + rb.listener(new RequestListener() { | |
| 417 | + @Override | |
| 418 | + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { | |
| 419 | + view.setScaleType(ImageView.ScaleType.CENTER); | |
| 420 | + if (loading != null) loading.setVisibility(View.GONE); | |
| 421 | + return false; | |
| 422 | + } | |
| 423 | + | |
| 424 | + @Override | |
| 425 | + public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { | |
| 426 | + if (loading != null) loading.setVisibility(View.GONE); | |
| 427 | + return false; | |
| 428 | + } | |
| 429 | + }); | |
| 430 | + | |
| 431 | + rb.apply(option).into(view); | |
| 432 | + } | |
| 433 | + | |
| 434 | + @Override | |
| 435 | + public void onError(Throwable e) { | |
| 436 | + if (loading != null) loading.setVisibility(View.GONE); | |
| 437 | + } | |
| 438 | + | |
| 439 | + @Override | |
| 440 | + public void onComplete() { | |
| 441 | + | |
| 442 | + } | |
| 443 | + }); | |
| 444 | + } | |
| 445 | + | |
| 446 | + private static void showVideoThumbnail(Context context, int mediaType, int id, String path, ImageView view, ProgressBar loading, RequestOptions option) { | |
| 447 | + Observable.create(new ObservableOnSubscribe<Bitmap>() { | |
| 448 | + @Override | |
| 449 | + public void subscribe(ObservableEmitter<Bitmap> emitter) throws Exception { | |
| 450 | + Bitmap bm = null; | |
| 451 | + if (mediaType == 1) { | |
| 452 | + bm = getImageThumbnail(context, id + ""); | |
| 453 | + emitter.onNext(bm); | |
| 454 | + } else if (mediaType == 3) { | |
| 455 | + bm = getVideoThumbnail(context, id + ""); | |
| 456 | + emitter.onNext(bm); | |
| 345 | 457 | } |
| 346 | - }); | |
| 347 | 458 | |
| 348 | - rb.apply(option).into(view); | |
| 349 | - start= System.currentTimeMillis(); | |
| 459 | + | |
| 460 | + } | |
| 461 | + }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Bitmap>() { | |
| 462 | + @Override | |
| 463 | + public void onSubscribe(Disposable d) { | |
| 464 | + | |
| 465 | + } | |
| 466 | + | |
| 467 | + @Override | |
| 468 | + public void onNext(Bitmap thumbnails) { | |
| 469 | + if (thumbnails != null) { | |
| 470 | + showThumbnail(context, thumbnails, view, loading, option); | |
| 471 | + } else { | |
| 472 | + showNoAndroidQ(context, mediaType, path, view, loading, option); | |
| 473 | + } | |
| 474 | + | |
| 475 | + } | |
| 476 | + | |
| 477 | + @Override | |
| 478 | + public void onError(Throwable e) { | |
| 479 | + if (loading != null) loading.setVisibility(View.GONE); | |
| 480 | + } | |
| 481 | + | |
| 482 | + @Override | |
| 483 | + public void onComplete() { | |
| 484 | + | |
| 485 | + } | |
| 486 | + }); | |
| 487 | + } | |
| 488 | + | |
| 489 | + | |
| 490 | + private static void showImageThumbnail(Context context, int mediaType, int id, String path, ImageView view, ProgressBar loading, RequestOptions option) { | |
| 491 | + Observable.create(new ObservableOnSubscribe<Bitmap>() { | |
| 492 | + @Override | |
| 493 | + public void subscribe(ObservableEmitter<Bitmap> emitter) throws Exception { | |
| 494 | + Bitmap bm = null; | |
| 495 | + if (mediaType == 1) { | |
| 496 | + bm = getImageThumbnail(context, id + ""); | |
| 497 | + emitter.onNext(bm); | |
| 498 | + } | |
| 499 | + | |
| 500 | + | |
| 501 | + } | |
| 502 | + }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Bitmap>() { | |
| 503 | + @Override | |
| 504 | + public void onSubscribe(Disposable d) { | |
| 505 | + | |
| 506 | + } | |
| 507 | + | |
| 508 | + @Override | |
| 509 | + public void onNext(Bitmap thumbnails) { | |
| 510 | + if (thumbnails != null) { | |
| 511 | + showThumbnail(context, thumbnails, view, loading, option); | |
| 512 | + } else { | |
| 513 | + showNoAndroidQ(context, mediaType, path, view, loading, option); | |
| 514 | + } | |
| 515 | + | |
| 516 | + } | |
| 517 | + | |
| 518 | + @Override | |
| 519 | + public void onError(Throwable e) { | |
| 520 | + if (loading != null) loading.setVisibility(View.GONE); | |
| 521 | + } | |
| 522 | + | |
| 523 | + @Override | |
| 524 | + public void onComplete() { | |
| 525 | + | |
| 526 | + } | |
| 527 | + }); | |
| 528 | + } | |
| 529 | + | |
| 530 | + private static void showThumbnail(Context context, Bitmap bm, ImageView view, ProgressBar loading, RequestOptions option) { | |
| 531 | + RequestBuilder rb = Glide.with(context).load(bm); | |
| 532 | + rb.listener(new RequestListener() { | |
| 533 | + @Override | |
| 534 | + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { | |
| 535 | + view.setScaleType(ImageView.ScaleType.CENTER); | |
| 536 | + if (loading != null) loading.setVisibility(View.GONE); | |
| 537 | + return false; | |
| 538 | + } | |
| 539 | + | |
| 540 | + @Override | |
| 541 | + public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { | |
| 542 | + if (loading != null) loading.setVisibility(View.GONE); | |
| 543 | + return false; | |
| 544 | + } | |
| 545 | + }); | |
| 546 | + | |
| 547 | + rb.apply(option).into(view); | |
| 548 | + } | |
| 549 | + | |
| 550 | + | |
| 551 | + public static Bitmap getVideoThumbnail(Context context, String videoId) { | |
| 552 | + Cursor thumbCursor = null; | |
| 553 | + Bitmap bitmap = null; | |
| 554 | + try { | |
| 555 | + thumbCursor = context.getApplicationContext().getContentResolver().query( | |
| 556 | + MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, | |
| 557 | + null, MediaStore.Video.Thumbnails.VIDEO_ID | |
| 558 | + + "=" + videoId, null, null); | |
| 559 | + if (thumbCursor.moveToFirst()) { | |
| 560 | + String albumPath = thumbCursor.getString(thumbCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA)); | |
| 561 | + bitmap = BitmapFactory.decodeFile(albumPath); | |
| 562 | + return bitmap; | |
| 563 | + } | |
| 350 | 564 | } catch (Exception e) { |
| 351 | - if (loading != null) loading.setVisibility(View.GONE); | |
| 565 | + return bitmap; | |
| 566 | + } finally { | |
| 567 | + if (thumbCursor != null) | |
| 568 | + thumbCursor.close(); | |
| 569 | + return bitmap; | |
| 570 | + } | |
| 571 | + } | |
| 572 | + | |
| 573 | + public static Bitmap getImageThumbnail(Context context, String videoId) { | |
| 574 | + Bitmap bitmap = null; | |
| 575 | + Cursor thumbCursor = null; | |
| 576 | + | |
| 577 | + | |
| 578 | +// String[] projection = {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID, | |
| 579 | +// MediaStore.Images.Thumbnails.DATA}; | |
| 580 | +// | |
| 581 | +// Cursor cursor = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null); | |
| 582 | +// | |
| 583 | + | |
| 584 | + try { | |
| 585 | + thumbCursor = context.getApplicationContext().getContentResolver().query( | |
| 586 | + MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, | |
| 587 | + null, MediaStore.Images.Thumbnails.IMAGE_ID | |
| 588 | + + "=" + videoId, null, null); | |
| 589 | + if (thumbCursor.moveToFirst()) { | |
| 590 | + String albumPath = thumbCursor.getString(thumbCursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA)); | |
| 591 | + bitmap = BitmapFactory.decodeFile(albumPath); | |
| 592 | + return bitmap; | |
| 593 | + } | |
| 594 | + } catch (Exception e) { | |
| 595 | + return bitmap; | |
| 596 | + } finally { | |
| 597 | + if (thumbCursor != null) | |
| 598 | + thumbCursor.close(); | |
| 599 | + return bitmap; | |
| 352 | 600 | } |
| 353 | 601 | } |
| 354 | 602 | |
| ... | ... | @@ -414,7 +662,7 @@ public class GlideCacheUtil { |
| 414 | 662 | RequestBuilder rb = null; |
| 415 | 663 | option = option |
| 416 | 664 | .dontTransform() |
| 417 | - .diskCacheStrategy(DiskCacheStrategy.RESOURCE) | |
| 665 | + .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC) | |
| 418 | 666 | .dontAnimate(); |
| 419 | 667 | option.error(R.drawable.iv_imgload_full_err); |
| 420 | 668 | ... | ... |
lib_media/src/main/java/com/cnlive/media/utils/ThumbnailsUtils.java
0 → 100644
| 1 | +package com.cnlive.media.utils; | |
| 2 | + | |
| 3 | +import android.content.Context; | |
| 4 | +import android.database.Cursor; | |
| 5 | +import android.net.Uri; | |
| 6 | +import android.provider.MediaStore; | |
| 7 | +import android.util.Log; | |
| 8 | + | |
| 9 | +import java.util.HashMap; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * 作 者 : wkq | |
| 13 | + * <p> | |
| 14 | + * 时 间 : 2021/1/25 15:18 | |
| 15 | + * <p> | |
| 16 | + * 名 字 : ThumbnailsUtils | |
| 17 | + * <p> | |
| 18 | + * 简 介 : | |
| 19 | + */ | |
| 20 | +public class ThumbnailsUtils { | |
| 21 | + static HashMap<String, String> thumbnails = new HashMap<String, String>(); | |
| 22 | + | |
| 23 | + public static void getThumbnails(Context context) { | |
| 24 | + String[] projection = {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID, | |
| 25 | + MediaStore.Images.Thumbnails.DATA}; | |
| 26 | + | |
| 27 | + Cursor cursor = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null); | |
| 28 | + if (cursor == null) return; | |
| 29 | + while (cursor.moveToNext()) { | |
| 30 | + int _id; | |
| 31 | + int image_id; | |
| 32 | + String image_path; | |
| 33 | + int _idColumn = cursor.getColumnIndex(MediaStore.Images.Thumbnails._ID); | |
| 34 | + int image_idColumn = cursor.getColumnIndex(MediaStore.Images.Thumbnails.IMAGE_ID); | |
| 35 | + int dataColumn = cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA); | |
| 36 | + _id = cursor.getInt(_idColumn); | |
| 37 | + image_id = cursor.getInt(image_idColumn); | |
| 38 | + image_path = cursor.getString(dataColumn); | |
| 39 | + Log.e("image_path:", _id+"-->"+image_id + "-->" + image_path); | |
| 40 | + thumbnails.put(image_id + "", image_path); | |
| 41 | + } | |
| 42 | + | |
| 43 | + | |
| 44 | + } | |
| 45 | + | |
| 46 | + | |
| 47 | +} | ... | ... |