• Android图像选择之 PictureSelector


    一款针对Android平台下的图片选择器,支持从相册获取图片、视频、音频&拍照,支持裁剪(单图or多图裁剪)、压缩、主题自定义配置等功能,支持动态获取权限&适配Android 5.0+系统的开源图片选择框架。

    废话不多。开干!!

    添加依赖:

        //图片选择器
        api 'io.github.lucksiege:pictureselector:v3.11.1'
        //图片压缩
        api 'io.github.lucksiege:compress:v3.11.1'
        //图片裁剪
        api 'io.github.lucksiege:ucrop:v3.11.1'
        //自定义相机
        api 'io.github.lucksiege:camerax:v3.11.1'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    实现工具类如下 PictureUtils :

    import android.Manifest;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.drawable.Drawable;
    import android.net.Uri;
    import android.provider.MediaStore;
    import android.widget.ImageView;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    
    import com.bumptech.glide.Glide;
    import com.bumptech.glide.request.target.CustomTarget;
    import com.bumptech.glide.request.transition.Transition;
    import com.luck.lib.camerax.SimpleCameraX;
    import com.luck.picture.lib.basic.PictureSelector;
    import com.luck.picture.lib.config.PictureMimeType;
    import com.luck.picture.lib.config.SelectMimeType;
    import com.luck.picture.lib.engine.CompressFileEngine;
    import com.luck.picture.lib.engine.CropFileEngine;
    import com.luck.picture.lib.engine.VideoPlayerEngine;
    import com.luck.picture.lib.entity.LocalMedia;
    import com.luck.picture.lib.entity.MediaExtraInfo;
    import com.luck.picture.lib.interfaces.OnCameraInterceptListener;
    import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener;
    import com.luck.picture.lib.interfaces.OnRecordAudioInterceptListener;
    import com.luck.picture.lib.interfaces.OnResultCallbackListener;
    import com.luck.picture.lib.permissions.PermissionChecker;
    import com.luck.picture.lib.permissions.PermissionResultCallback;
    import com.luck.picture.lib.utils.MediaUtils;
    import com.luck.picture.lib.utils.ToastUtils;
    import com.yalantis.ucrop.UCrop;
    import com.yalantis.ucrop.UCropImageEngine;
    
    import java.io.File;
    import java.util.ArrayList;
    
    import top.zibin.luban.Luban;
    import top.zibin.luban.OnNewCompressListener;
    
    /**
     * @author: zzw
     * @Description:
     * @Date: 2022/9/21 21:37
     */
    public class PictureUtils {
    
        /**
         * 打开摄像头 拍照
         *
         * @param context
         * @param isRotateImage                   true 前置 false 后置
         * @param onPictureSelectorResultListener 回调
         */
        public static void openCamera(Context context, boolean isRotateImage, OnPictureSelectorResultListener onPictureSelectorResultListener) {
            openCamera(context, SelectMimeType.ofImage(), isRotateImage, onPictureSelectorResultListener);
        }
    
        /**
         * 打开摄像头 录制视频
         *
         * @param context
         * @param isRotateImage                   true 前置 false 后置
         * @param onPictureSelectorResultListener 回调
         */
        public static void openVideo(Context context, boolean isRotateImage, OnPictureSelectorResultListener onPictureSelectorResultListener) {
            openCamera(context, SelectMimeType.ofVideo(), isRotateImage, onPictureSelectorResultListener);
        }
    
        /**
         * 打开摄像头
         *
         * @param context                         上下文
         * @param onPictureSelectorResultListener 回调
         */
        public static void openCamera(Context context, int openCamera, boolean isRotateImage, OnPictureSelectorResultListener onPictureSelectorResultListener) {
            PictureSelector.create(context)
                    .openCamera(openCamera)
                    .isCameraAroundState(isRotateImage)
                    .setVideoThumbnailListener(new VideoThumbListener(context))
                    .setCompressEngine((CompressFileEngine) (context1, source, call) -> Luban.with(context1).load(source).ignoreBy(100)
                            .setCompressListener(new OnNewCompressListener() {
                                @Override
                                public void onStart() {
    
                                }
    
                                @Override
                                public void onSuccess(String source, File compressFile) {
                                    if (call != null) {
                                        call.onCallback(source, compressFile.getAbsolutePath());
                                    }
                                }
    
                                @Override
                                public void onError(String source, Throwable e) {
                                    if (call != null) {
                                        call.onCallback(source, null);
                                    }
                                }
                            }).launch())
                    .forResult(new OnResultCallbackListener<LocalMedia>() {
                        @Override
                        public void onResult(ArrayList<LocalMedia> result) {
                            onPictureSelectorResultListener.onResult(result);
                        }
    
                        @Override
                        public void onCancel() {
                        }
                    });
        }
    
        /**
         * 设置头像
         *
         * @param mContext
         * @param selectResult                    结果
         * @param onPictureSelectorResultListener 结果回调
         */
        public static void createAvatar(Context mContext, ArrayList<LocalMedia> selectResult, OnPictureSelectorResultListener onPictureSelectorResultListener) {
            create(mContext, SelectMimeType.ofImage(), selectResult, 1, 1, true, onPictureSelectorResultListener);
        }
    
        /**
         * 选择单张图片
         *
         * @param mContext
         * @param selectResult                    结果
         * @param onPictureSelectorResultListener 结果回调
         */
        public static void createImageMin(Context mContext, ArrayList<LocalMedia> selectResult, OnPictureSelectorResultListener onPictureSelectorResultListener) {
            create(mContext, SelectMimeType.ofImage(), selectResult, 1, 1, false, onPictureSelectorResultListener);
        }
    
    
        /**
         * 选择多张图片
         *
         * @param mContext
         * @param selectResult                    结果
         * @param selectMax                       最多选择
         * @param onPictureSelectorResultListener 结果回调
         */
        public static void createImageMax(Context mContext, int selectMax, ArrayList<LocalMedia> selectResult, OnPictureSelectorResultListener onPictureSelectorResultListener) {
            create(mContext, SelectMimeType.ofImage(), selectResult, 1, selectMax, false, onPictureSelectorResultListener);
        }
    
        /**
         * 选择单个视频
         *
         * @param mContext
         * @param selectResult                    结果
         * @param onPictureSelectorResultListener 结果回调
         */
        public static void createVideo(Context mContext, ArrayList<LocalMedia> selectResult, OnPictureSelectorResultListener onPictureSelectorResultListener) {
            create(mContext, SelectMimeType.ofVideo(), selectResult, 1, 1, false, onPictureSelectorResultListener);
        }
    
        /**
         * 选择单个音频
         *
         * @param mContext
         * @param selectResult                    结果
         * @param onPictureSelectorResultListener 结果回调
         */
        public static void createAudio(Context mContext, ArrayList<LocalMedia> selectResult, OnPictureSelectorResultListener onPictureSelectorResultListener) {
            create(mContext, SelectMimeType.ofAudio(), selectResult, 1, 1, false, onPictureSelectorResultListener);
        }
    
        /**
         * 选择 媒体 图片 视频 音频
         *
         * @param mContext
         * @param selectResult                    结果
         * @param onPictureSelectorResultListener 结果回调
         */
        public static void createPicture(Context mContext, ArrayList<LocalMedia> selectResult, OnPictureSelectorResultListener onPictureSelectorResultListener) {
            create(mContext, SelectMimeType.ofAll(), selectResult, 1, 1, false, onPictureSelectorResultListener);
        }
    
        /**
         * 默认设置
         *
         * @param mContext
         * @param selectMimeType                  结果
         * @param selectResult                    结果
         * @param selectMin                       最少选择
         * @param selectMax                       最多选择
         * @param isCrop                          是否剪裁
         * @param onPictureSelectorResultListener 结果回到
         */
        public static void create(Context mContext, int selectMimeType, ArrayList<LocalMedia> selectResult, int selectMin, int selectMax, boolean isCrop,
                                  OnPictureSelectorResultListener onPictureSelectorResultListener) {
            PictureSelector.create(mContext)
                    .openGallery(selectMimeType)
                    .setMaxSelectNum(selectMax)
                    .setCropEngine(getCropFileEngine(isCrop))
                    .setMinSelectNum(selectMin)
                    .setFilterVideoMaxSecond(selectMimeType == SelectMimeType.ofVideo() ? 60 : 60 * 10)
                    .setFilterVideoMinSecond(5)
                    .setRecordVideoMaxSecond(selectMimeType == SelectMimeType.ofVideo() ? 60 : 60 * 10)
                    .setRecordVideoMinSecond(5)
                    .setFilterMaxFileSize(100 * 1024 * 1024)
                    .setCameraInterceptListener(new MeOnCameraInterceptListener())
                    .isFilterSizeDuration(true)
                    .setSelectedData(selectResult)
                    .setRecordAudioInterceptListener(new MeOnRecordAudioInterceptListener())
                    .setCompressEngine((CompressFileEngine) (context, source, call) -> Luban.with(context).load(source).ignoreBy(100)
                            .setCompressListener(new OnNewCompressListener() {
                                @Override
                                public void onStart() {
    
                                }
    
                                @Override
                                public void onSuccess(String source, File compressFile) {
                                    if (call != null) {
                                        call.onCallback(source, compressFile.getAbsolutePath());
                                    }
                                }
    
                                @Override
                                public void onError(String source, Throwable e) {
                                    if (call != null) {
                                        call.onCallback(source, null);
                                    }
                                }
                            }).launch()).setImageEngine(GlideUtils.createGlideEngine())
                    .forResult(new OnResultCallbackListener<LocalMedia>() {
                        @Override
                        public void onResult(ArrayList<LocalMedia> result) {
                            for (LocalMedia media : result) {
                                if (media.getWidth() == 0 || media.getHeight() == 0) {
                                    if (PictureMimeType.isHasImage(media.getMimeType())) {
                                        MediaExtraInfo imageExtraInfo = MediaUtils.getImageSize(mContext, media.getPath());
                                        media.setWidth(imageExtraInfo.getWidth());
                                        media.setHeight(imageExtraInfo.getHeight());
                                    } else if (PictureMimeType.isHasVideo(media.getMimeType())) {
                                        MediaExtraInfo videoExtraInfo = MediaUtils.getVideoSize(mContext, media.getPath());
                                        media.setWidth(videoExtraInfo.getWidth());
                                        media.setHeight(videoExtraInfo.getHeight());
                                    }
                                }
    //                            LogUtils.e("文件名: " + media.getFileName() + "\n" +
    //                                    "是否压缩:" + media.isCompressed() + "\n" +
    //                                    "压缩路径:" + media.getCompressPath() + "\n" +
    //                                    "初始路径:" + media.getPath() + "\n" +
    //                                    "绝对路径:" + media.getRealPath() + "\n" +
    //                                    "是否裁剪:" + media.isCut() + "\n" +
    //                                    "裁剪路径:" + media.getCutPath() + "\n" +
    //                                    "是否开启原图:" + media.isOriginal() + "\n" +
    //                                    "原图路径:" + media.getOriginalPath() + "\n" +
    //                                    "沙盒路径:" + media.getSandboxPath() + "\n" +
    //                                    "水印路径:" + media.getWatermarkPath() + "\n" +
    //                                    "视频缩略图:" + media.getVideoThumbnailPath() + "\n" +
    //                                    "原始宽高: " + media.getWidth() + "x" + media.getHeight() + "\n" +
    //                                    "裁剪宽高: " + media.getCropImageWidth() + "x" + media.getCropImageHeight() + "\n" +
    //                                    "文件大小: " + PictureFileUtils.formatAccurateUnitFileSize(media.getSize()) + "\n" +
    //                                    "文件大小: " + media.getSize() + "\n" +
    //                                    "文件时长: " + media.getDuration()
    //                            );
                            }
                            onPictureSelectorResultListener.onResult(result);
                        }
    
                        @Override
                        public void onCancel() {
    
                        }
                    });
        }
    
        /**
         * 查看图片大图
         *
         * @param mContext
         * @param position
         * @param localMedia
         */
        public static void openImage(Context mContext, int position, ArrayList<LocalMedia> localMedia) {
            PictureSelector.create(mContext)
                    .openPreview()
                    .isHidePreviewDownload(true)
                    .setImageEngine(GlideUtils.createGlideEngine())
                    .setExternalPreviewEventListener(new OnExternalPreviewEventListener() {
                        @Override
                        public void onPreviewDelete(int position) {
    
                        }
    
                        @Override
                        public boolean onLongPressDownload(Context context, LocalMedia media) {
                            return false;
                        }
    
                    }).startActivityPreview(position, false, localMedia);
        }
    
        public static void openImage(Context mContext, int position, String imageUrl) {
            ArrayList<LocalMedia> localMedia = new ArrayList<>();
            for (String url : imageUrl.split(",")) {
                localMedia.add(LocalMedia.generateHttpAsLocalMedia(url));
            }
            PictureSelector.create(mContext)
                    .openPreview()
                    .setImageEngine(GlideUtils.createGlideEngine())
                    .setExternalPreviewEventListener(new OnExternalPreviewEventListener() {
                        @Override
                        public void onPreviewDelete(int position) {
    
                        }
    
                        @Override
                        public boolean onLongPressDownload(Context context, LocalMedia media) {
                            return false;
                        }
    
                    }).startActivityPreview(position, false, localMedia);
        }
    
        /**
         * 预览视频
         *
         * @param mContext
         * @param position
         * @param localMedia
         */
        public static void openVideo(Context mContext, int position, ArrayList<LocalMedia> localMedia) {
            openVideo(mContext, position, localMedia, null);
        }
    
        /**
         * 预览视频
         *
         * @param mContext
         * @param position
         * @param localMedia
         */
        public static void openVideo(Context mContext, int position, ArrayList<LocalMedia> localMedia, VideoPlayerEngine videoPlayerEngine) {
            PictureSelector.create(mContext)
                    .openPreview()
                    .setImageEngine(GlideUtils.createGlideEngine())
                    .setVideoPlayerEngine(videoPlayerEngine)
                    .isAutoVideoPlay(true)
                    .setExternalPreviewEventListener(new OnExternalPreviewEventListener() {
                        @Override
                        public void onPreviewDelete(int position) {
    
                        }
    
                        @Override
                        public boolean onLongPressDownload(Context context, LocalMedia media) {
                            return false;
                        }
    
                    }).startActivityPreview(position, false, localMedia);
        }
    
    
        /**
         * 裁剪引擎
         *
         * @return
         */
        private static ImageFileCropEngine getCropFileEngine(boolean isCrop) {
            return isCrop ? new ImageFileCropEngine() : null;
        }
    
        /**
         * 自定义裁剪
         */
        private static class ImageFileCropEngine implements CropFileEngine {
    
            @Override
            public void onStartCrop(Fragment fragment, Uri srcUri, Uri destinationUri, ArrayList<String> dataSource, int requestCode) {
                UCrop.Options options = buildOptions();
                UCrop uCrop = UCrop.of(srcUri, destinationUri, dataSource);
                uCrop.withOptions(options);
                uCrop.setImageEngine(new UCropImageEngine() {
                    @Override
                    public void loadImage(Context context, String url, ImageView imageView) {
                        Glide.with(context).load(url).override(180, 180).into(imageView);
                    }
    
                    @Override
                    public void loadImage(Context context, Uri url, int maxWidth, int maxHeight, OnCallbackListener<Bitmap> call) {
                        Glide.with(context)
                                .asBitmap()
                                .load(url)
                                .override(maxWidth, maxHeight)
                                .into(new CustomTarget<Bitmap>() {
                                    @Override
                                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                        if (call != null) {
                                            call.onCall(resource);
                                        }
                                    }
    
                                    @Override
                                    public void onLoadCleared(@Nullable Drawable placeholder) {
                                        if (call != null) {
                                            call.onCall(null);
                                        }
                                    }
                                });
                    }
                });
                uCrop.start(fragment.requireActivity(), fragment, requestCode);
            }
        }
    
    
        /**
         * 配制UCrop,可根据需求自我扩展
         *
         * @return
         */
        private static UCrop.Options buildOptions() {
            UCrop.Options options = new UCrop.Options();
            options.setHideBottomControls(true);
            options.setFreeStyleCropEnabled(true);
            options.setShowCropFrame(true);
            options.setShowCropGrid(false);
            options.setCircleDimmedLayer(false);
            options.withAspectRatio(1, 1);
            options.isCropDragSmoothToCenter(false);
            options.setMaxScaleMultiplier(100);
            return options;
        }
    
    
        public interface OnPictureSelectorResultListener {
            void onResult(ArrayList<LocalMedia> result);
        }
    
        /**
         * 自定义拍照
         */
        private static class MeOnCameraInterceptListener implements OnCameraInterceptListener {
    
            @Override
            public void openCamera(Fragment fragment, int cameraMode, int requestCode) {
                SimpleCameraX camera = SimpleCameraX.of();
                camera.isAutoRotation(true);
                camera.setCameraMode(cameraMode);
                camera.setVideoFrameRate(50);
                camera.setVideoBitRate(5 * 1024 * 1024);
                camera.isDisplayRecordChangeTime(true);
                camera.isManualFocusCameraPreview(true);
                camera.isZoomCameraPreview(true);
                camera.setImageEngine((context, url, imageView) -> Glide.with(context).load(url).into(imageView));
                camera.start(fragment.requireActivity(), fragment, requestCode);
            }
        }
    
        /**
         * 录音回调事件
         */
        private static class MeOnRecordAudioInterceptListener implements OnRecordAudioInterceptListener {
    
            @Override
            public void onRecordAudio(Fragment fragment, int requestCode) {
                String[] recordAudio = {Manifest.permission.RECORD_AUDIO};
                if (PermissionChecker.isCheckSelfPermission(fragment.getContext(), recordAudio)) {
                    startRecordSoundAction(fragment, requestCode);
                } else {
                    PermissionChecker.getInstance().requestPermissions(fragment,
                            new String[]{Manifest.permission.RECORD_AUDIO}, new PermissionResultCallback() {
                                @Override
                                public void onGranted() {
                                    startRecordSoundAction(fragment, requestCode);
                                }
    
                                @Override
                                public void onDenied() {
                                }
                            });
                }
            }
        }
    
        /**
         * 启动录音意图
         *
         * @param fragment
         * @param requestCode
         */
        private static void startRecordSoundAction(Fragment fragment, int requestCode) {
            Intent recordAudioIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
            if (recordAudioIntent.resolveActivity(fragment.requireActivity().getPackageManager()) != null) {
                fragment.startActivityForResult(recordAudioIntent, requestCode);
            } else {
                ToastUtils.showToast(fragment.getContext(), "The system is missing a recording component");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500

    上文监听 VideoThumbListener:

    
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.drawable.Drawable;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    
    import com.bumptech.glide.Glide;
    import com.bumptech.glide.request.target.CustomTarget;
    import com.bumptech.glide.request.transition.Transition;
    import com.luck.picture.lib.interfaces.OnKeyValueResultCallbackListener;
    import com.luck.picture.lib.interfaces.OnVideoThumbnailEventListener;
    import com.luck.picture.lib.utils.PictureFileUtils;
    
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     *
     * @Description: 视频缩略图
     * 
     */
    public class VideoThumbListener implements OnVideoThumbnailEventListener {
    
        private Context context;
    
        public VideoThumbListener(Context context) {
            this.context = context;
        }
    
        @Override
        public void onVideoThumbnail(Context context, String videoPath, OnKeyValueResultCallbackListener call) {
            Glide.with(context).asBitmap().sizeMultiplier(0.6F).load(videoPath).into(new CustomTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    resource.compress(Bitmap.CompressFormat.JPEG, 60, stream);
                    FileOutputStream fos = null;
                    String result = null;
                    try {
                        File targetFile = new File(getVideoThumbnailDir(), "thumbnails_" + System.currentTimeMillis() + ".jpg");
                        fos = new FileOutputStream(targetFile);
                        fos.write(stream.toByteArray());
                        fos.flush();
                        result = targetFile.getAbsolutePath();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        PictureFileUtils.close(fos);
                        PictureFileUtils.close(stream);
                    }
                    if (call != null) {
                        call.onCallback(videoPath, result);
                    }
                }
    
                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {
                    if (call != null) {
                        call.onCallback(videoPath, "");
                    }
                }
            });
        }
    
        private String getVideoThumbnailDir() {
            File externalFilesDir = context.getExternalFilesDir("");
            File customFile = new File(externalFilesDir.getAbsolutePath(), "Thumbnail");
            if (!customFile.exists()) {
                customFile.mkdirs();
            }
            return customFile.getAbsolutePath() + File.separator;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    使用:

    使用摄像头拍照:

    PictureUtils.openCamera(requireActivity(), false) {
    	val localMedia = it[0]
                   
    }
    
    • 1
    • 2
    • 3
    • 4

    选择单张图片

      PictureUtils.createImageMin(this, ArrayList()) {
    		val localMedia = it[0]
                  
    }
    
    • 1
    • 2
    • 3
    • 4

    选择多张图片

    PictureUtils.createImageMax(this, 9, arrayListOf()) { result: ArrayList<LocalMedia> ->
       ...                         
    }
    
    • 1
    • 2
    • 3

    简单使用如上。
    END

  • 相关阅读:
    Mongodbd的简学
    Python 教程之控制流(3)Python 中的循环和控制语句(继续、中断和通过)
    医院能耗管理平台:实现能源消耗的全面掌控
    Qt QSplitter拆分器
    在macOS上实现多进程任务处理
    【数据结构与算法】---OJ链表题来源力扣和牛客
    JDBC介绍
    Python实现类别变量的独热编码(One-hot Encoding)
    【深度学习实验】卷积神经网络(六):自定义卷积神经网络模型(VGG)实现图片多分类任务
    LeetCode 6254. 划分技能点相等的团队
  • 原文地址:https://blog.csdn.net/lixinxiaos/article/details/132899394