-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathMediaGridFragment.java
888 lines (789 loc) · 35.7 KB
/
MediaGridFragment.java
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
package cn.finalteam.rxgalleryfinal.ui.fragment;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.yalantis.ucrop.UCrop;
import com.yalantis.ucrop.UCropActivity;
import com.yalantis.ucrop.model.AspectRatio;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import cn.finalteam.rxgalleryfinal.Configuration;
import cn.finalteam.rxgalleryfinal.R;
import cn.finalteam.rxgalleryfinal.anim.Animation;
import cn.finalteam.rxgalleryfinal.anim.SlideInUnderneathAnimation;
import cn.finalteam.rxgalleryfinal.anim.SlideOutUnderneathAnimation;
import cn.finalteam.rxgalleryfinal.bean.BucketBean;
import cn.finalteam.rxgalleryfinal.bean.ImageCropBean;
import cn.finalteam.rxgalleryfinal.bean.MediaBean;
import cn.finalteam.rxgalleryfinal.presenter.impl.MediaGridPresenterImpl;
import cn.finalteam.rxgalleryfinal.rxbus.RxBus;
import cn.finalteam.rxgalleryfinal.rxbus.RxBusDisposable;
import cn.finalteam.rxgalleryfinal.rxbus.event.CloseMediaViewPageFragmentEvent;
import cn.finalteam.rxgalleryfinal.rxbus.event.ImageRadioResultEvent;
import cn.finalteam.rxgalleryfinal.rxbus.event.MediaCheckChangeEvent;
import cn.finalteam.rxgalleryfinal.rxbus.event.OpenMediaPageFragmentEvent;
import cn.finalteam.rxgalleryfinal.rxbus.event.OpenMediaPreviewFragmentEvent;
import cn.finalteam.rxgalleryfinal.rxbus.event.RequestStorageReadAccessPermissionEvent;
import cn.finalteam.rxgalleryfinal.ui.activity.MediaActivity;
import cn.finalteam.rxgalleryfinal.ui.adapter.BucketAdapter;
import cn.finalteam.rxgalleryfinal.ui.adapter.MediaGridAdapter;
import cn.finalteam.rxgalleryfinal.ui.base.IRadioImageCheckedListener;
import cn.finalteam.rxgalleryfinal.ui.widget.FooterAdapter;
import cn.finalteam.rxgalleryfinal.ui.widget.HorizontalDividerItemDecoration;
import cn.finalteam.rxgalleryfinal.ui.widget.MarginDecoration;
import cn.finalteam.rxgalleryfinal.ui.widget.RecyclerViewFinal;
import cn.finalteam.rxgalleryfinal.utils.CameraUtils;
import cn.finalteam.rxgalleryfinal.utils.DeviceUtils;
import cn.finalteam.rxgalleryfinal.utils.EmptyViewUtils;
import cn.finalteam.rxgalleryfinal.utils.FileUtils;
import cn.finalteam.rxgalleryfinal.utils.Logger;
import cn.finalteam.rxgalleryfinal.utils.MediaScanner;
import cn.finalteam.rxgalleryfinal.utils.MediaUtils;
import cn.finalteam.rxgalleryfinal.utils.PermissionCheckUtils;
import cn.finalteam.rxgalleryfinal.utils.SimpleDateUtils;
import cn.finalteam.rxgalleryfinal.utils.ThemeUtils;
import cn.finalteam.rxgalleryfinal.view.MediaGridView;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
/**
* Desction:
* Author:pengjianbo Dujinyang
* Date:16/5/7 上午10:02
* <p>
* Desction: 直接暴漏
* Author:KARL-Dujinyang
* Date:2017.
*/
public class MediaGridFragment extends BaseFragment implements MediaGridView, RecyclerViewFinal.OnLoadMoreListener,
FooterAdapter.OnItemClickListener, View.OnClickListener, MediaScanner.ScanCallback, BucketAdapter.OnRecyclerViewItemClickListener {
private static final String IMAGE_TYPE = "image/jpeg";
//接口-单选-是否裁剪
public static IRadioImageCheckedListener iListenerRadio;
//预留公开命名接口
private static File mImageStoreDir;
private static File mImageStoreCropDir; //裁剪目录
//裁剪后+name
private static File mCropPath = null;
private final String IMAGE_STORE_FILE_NAME = "IMG_%s.jpg";
private final String VIDEO_STORE_FILE_NAME = "IMG_%s.mp4";
private final int TAKE_IMAGE_REQUEST_CODE = 1001;
private final int CROP_IMAGE_REQUEST_CODE = 1011;
private final String TAKE_URL_STORAGE_KEY = "take_url_storage_key";
private final String BUCKET_ID_KEY = "bucket_id_key";
private final int LIMIT = 23;
MediaGridPresenterImpl mMediaGridPresenter;
DisplayMetrics mScreenSize;
private List<MediaBean> mMediaBeanList;
private MediaGridAdapter mMediaGridAdapter;
private RecyclerViewFinal mRvMedia;
private LinearLayout mLlEmptyView;
private RecyclerView mRvBucket;
private BucketAdapter mBucketAdapter;
private RelativeLayout mRlBucektOverview;
private List<BucketBean> mBucketBeanList;
private TextView mTvFolderName;
private TextView mTvPreview;
private RelativeLayout mRlRootView;
//扫描
private MediaScanner mMediaScanner;
private int mPage = 1;
private String mImagePath;
private String mBucketId = String.valueOf(Integer.MIN_VALUE);
private MediaActivity mMediaActivity;
private Disposable mMediaCheckChangeDisposable;
private Disposable mCloseMediaViewPageFragmentDisposable;
private Disposable mRequestStorageReadAccessPermissionDisposable;
private SlideInUnderneathAnimation slideInUnderneathAnimation;
private SlideOutUnderneathAnimation slideOutUnderneathAnimation;
private int uCropStatusColor;
private int uCropToolbarColor;
private int uCropActivityWidgetColor;
private int uCropToolbarWidgetColor;
private String uCropTitle;
private String requestStorageAccessPermissionTips;
public static MediaGridFragment newInstance(Configuration configuration) {
MediaGridFragment fragment = new MediaGridFragment();
Bundle bundle = new Bundle();
bundle.putParcelable(EXTRA_CONFIGURATION, configuration);
fragment.setArguments(bundle);
return fragment;
}
/**
* getImageStoreDir
*
* @return 存储路径
*/
public static File getImageStoreDirByFile() {
return mImageStoreDir;
}
/**
* getImageStoreDir
*
* @return 存储路径
*/
public static String getImageStoreDirByStr() {
if (mImageStoreDir != null)
return mImageStoreDir.getPath();
else
return null;
}
/**
* 设置路径
*/
public static void setImageStoreDir(File imgFile) {
Logger.i("设置图片保存路径为:" + imgFile.getAbsolutePath());
mImageStoreDir = imgFile;
}
/**
* 设置路径
*/
public static void setImageStoreDir(String imgFile) {
mImageStoreDir = new File(Environment.getExternalStorageDirectory(), "/DCIM" + File.separator + imgFile + File.separator);
Logger.i("设置图片保存路径为:" + mImageStoreDir.getAbsolutePath());
}
/**
* getImageStoreDir裁剪
*
* @return 裁剪存储路径
*/
public static File getImageStoreCropDirByFile() {
return mImageStoreCropDir;
}
/**
* getImageStoreDir
*
* @return 存储路径
*/
public static String getImageStoreCropDirByStr() {
if (mImageStoreCropDir != null)
return mImageStoreCropDir.getPath();
else
return null;
}
/**
* 设置裁剪路径
*/
public static void setImageStoreCropDir(File imgFile) {
mImageStoreCropDir = imgFile;
Logger.i("设置图片裁剪保存路径为:" + mImageStoreCropDir.getAbsolutePath());
}
/**
* 设置裁剪路径
*
* @param imgFile 裁剪
*/
public static void setImageStoreCropDir(String imgFile) {
mImageStoreCropDir = new File(Environment.getExternalStorageDirectory(), "/DCIM" + File.separator + imgFile + File.separator);
if (!mImageStoreCropDir.exists()) {
mImageStoreCropDir.mkdirs();
}
Logger.i("设置图片裁剪保存路径为:" + mImageStoreCropDir.getAbsolutePath());
}
public static void setRadioListener(IRadioImageCheckedListener radioListener) {
MediaGridFragment.iListenerRadio = radioListener;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof MediaActivity) {
mMediaActivity = (MediaActivity) context;
}
mMediaScanner = new MediaScanner(context);
}
@Override
public int getContentView() {
return R.layout.gallery_fragment_media_grid;
}
@Override
public void onViewCreatedOk(View view, @Nullable Bundle savedInstanceState) {
mRvMedia = (RecyclerViewFinal) view.findViewById(R.id.rv_media);
mLlEmptyView = (LinearLayout) view.findViewById(R.id.ll_empty_view);
mRvBucket = (RecyclerView) view.findViewById(R.id.rv_bucket);
mRlBucektOverview = (RelativeLayout) view.findViewById(R.id.rl_bucket_overview);
mRlRootView = (RelativeLayout) view.findViewById(R.id.rl_root_view);
mRvMedia.setEmptyView(mLlEmptyView);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
mRvMedia.addItemDecoration(new MarginDecoration(getContext()));
mRvMedia.setLayoutManager(gridLayoutManager);
mRvMedia.setOnLoadMoreListener(this);
mRvMedia.setFooterViewHide(true);
mTvFolderName = (TextView) view.findViewById(R.id.tv_folder_name);
mTvFolderName.setOnClickListener(this);
mTvPreview = (TextView) view.findViewById(R.id.tv_preview);
mTvPreview.setOnClickListener(this);
mTvPreview.setEnabled(false);
if (mConfiguration.isRadio()) {
view.findViewById(R.id.tv_preview_vr).setVisibility(View.GONE);
mTvPreview.setVisibility(View.GONE);
} else {
if (mConfiguration.isHidePreview()) {
view.findViewById(R.id.tv_preview_vr).setVisibility(View.GONE);
mTvPreview.setVisibility(View.GONE);
} else {
view.findViewById(R.id.tv_preview_vr).setVisibility(View.VISIBLE);
mTvPreview.setVisibility(View.VISIBLE);
}
}
mMediaBeanList = new ArrayList<>();
mScreenSize = DeviceUtils.getScreenSize(getContext());
mMediaGridAdapter = new MediaGridAdapter(mMediaActivity, mMediaBeanList, mScreenSize.widthPixels, mConfiguration);
mRvMedia.setAdapter(mMediaGridAdapter);
mMediaGridPresenter = new MediaGridPresenterImpl(getContext(), mConfiguration.isImage());
mMediaGridPresenter.setMediaGridView(this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
mRvBucket.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getContext())
.color(getResources().getColor(R.color.gallery_bucket_list_decoration_color))
.size(getResources().getDimensionPixelSize(R.dimen.gallery_divider_decoration_height))
.margin(getResources().getDimensionPixelSize(R.dimen.gallery_bucket_margin),
getResources().getDimensionPixelSize(R.dimen.gallery_bucket_margin))
.build());
mRvBucket.setLayoutManager(linearLayoutManager);
mBucketBeanList = new ArrayList<>();
mBucketAdapter = new BucketAdapter(mBucketBeanList, mConfiguration, ContextCompat.getColor(getContext(), R.color.gallery_bucket_list_item_normal_color));
mRvBucket.setAdapter(mBucketAdapter);
mRvMedia.setOnItemClickListener(this);
mMediaGridPresenter.getBucketList();
mBucketAdapter.setOnRecyclerViewItemClickListener(this);
mRlBucektOverview.setVisibility(View.INVISIBLE);
if (slideInUnderneathAnimation == null) {
slideInUnderneathAnimation = new SlideInUnderneathAnimation(mRvBucket);
}
slideInUnderneathAnimation
.setDirection(Animation.DIRECTION_DOWN)
.animate();
subscribeEvent();
Activity activity = mMediaActivity;
if (activity == null) {
activity = getActivity();
}
if (mConfiguration.isImage()) {
mTvFolderName.setText(R.string.gallery_all_image);
} else {
mTvFolderName.setText(R.string.gallery_all_video);
}
String requestStorageAccessPermissionTips = ThemeUtils.resolveString(getContext(),
R.attr.gallery_request_storage_access_permission_tips,
R.string.gallery_default_request_storage_access_permission_tips);
boolean success = PermissionCheckUtils.checkReadExternalPermission(activity, requestStorageAccessPermissionTips,
MediaActivity.REQUEST_STORAGE_READ_ACCESS_PERMISSION);
if (success) {
mMediaGridPresenter.getMediaList(mBucketId, mPage, LIMIT);
}
}
/**
* RxBus
*/
private void subscribeEvent() {
mMediaCheckChangeDisposable = RxBus.getDefault().toObservable(MediaCheckChangeEvent.class)
.subscribeWith(new RxBusDisposable<MediaCheckChangeEvent>() {
@Override
protected void onEvent(MediaCheckChangeEvent mediaCheckChangeEvent) {
if (mMediaActivity.getCheckedList().size() == 0) {
mTvPreview.setEnabled(false);
} else {
mTvPreview.setEnabled(true);
}
}
});
RxBus.getDefault().add(mMediaCheckChangeDisposable);
mCloseMediaViewPageFragmentDisposable = RxBus.getDefault().toObservable(CloseMediaViewPageFragmentEvent.class)
.subscribeWith(new RxBusDisposable<CloseMediaViewPageFragmentEvent>() {
@Override
protected void onEvent(CloseMediaViewPageFragmentEvent closeMediaViewPageFragmentEvent) throws Exception {
mMediaGridAdapter.notifyDataSetChanged();
}
});
RxBus.getDefault().add(mCloseMediaViewPageFragmentDisposable);
mRequestStorageReadAccessPermissionDisposable = RxBus.getDefault().toObservable(RequestStorageReadAccessPermissionEvent.class)
.subscribeWith(new RxBusDisposable<RequestStorageReadAccessPermissionEvent>() {
@Override
protected void onEvent(RequestStorageReadAccessPermissionEvent requestStorageReadAccessPermissionEvent) throws Exception {
if (requestStorageReadAccessPermissionEvent.getType() == RequestStorageReadAccessPermissionEvent.TYPE_WRITE) {
if (requestStorageReadAccessPermissionEvent.isSuccess()) {
mMediaGridPresenter.getMediaList(mBucketId, mPage, LIMIT);
} else {
getActivity().finish();
}
} else {
if (requestStorageReadAccessPermissionEvent.isSuccess()) {
openCamera(mMediaActivity);
}
}
}
});
RxBus.getDefault().add(mRequestStorageReadAccessPermissionDisposable);
}
/**
* 设置主题
*/
@Override
public void setTheme() {
super.setTheme();
uCropStatusColor = ThemeUtils.resolveColor(getActivity(), R.attr.gallery_ucrop_status_bar_color, R.color.gallery_default_ucrop_color_widget_active);
uCropToolbarColor = ThemeUtils.resolveColor(getActivity(), R.attr.gallery_ucrop_toolbar_color, R.color.gallery_default_ucrop_color_widget_active);
uCropActivityWidgetColor = ThemeUtils.resolveColor(getActivity(), R.attr.gallery_ucrop_activity_widget_color, R.color.gallery_default_ucrop_color_widget);
uCropToolbarWidgetColor = ThemeUtils.resolveColor(getActivity(), R.attr.gallery_ucrop_toolbar_widget_color, R.color.gallery_default_toolbar_widget_color);
uCropTitle = ThemeUtils.resolveString(getActivity(), R.attr.gallery_ucrop_toolbar_title, R.string.gallery_edit_phote);
int pageColor = ThemeUtils.resolveColor(getContext(), R.attr.gallery_page_bg, R.color.gallery_default_page_bg);
mRlRootView.setBackgroundColor(pageColor);
requestStorageAccessPermissionTips = ThemeUtils.resolveString(getContext(), R.attr.gallery_request_camera_permission_tips, R.string.gallery_default_camera_access_permission_tips);
}
@Override
protected void onFirstTimeLaunched() {
}
@Override
public void onStart() {
super.onStart();
onLoadFile();
//直接刷新一次
// refreshUI();
}
@Override
public void loadMore() {
mMediaGridPresenter.getMediaList(mBucketId, mPage, LIMIT);
}
@Override
public void onRequestMediaCallback(List<MediaBean> list) {
if (!mConfiguration.isHideCamera()) {
if (mPage == 1 && TextUtils.equals(mBucketId, String.valueOf(Integer.MIN_VALUE))) {
MediaBean takePhotoBean = new MediaBean();
takePhotoBean.setId(Integer.MIN_VALUE);
takePhotoBean.setBucketId(String.valueOf(Integer.MIN_VALUE));
mMediaBeanList.add(takePhotoBean);
}
}
if (list != null && list.size() > 0) {
mMediaBeanList.addAll(list);
Logger.i(String.format("得到:%s张图片", list.size()));
} else {
Logger.i("没有更多图片");
}
mMediaGridAdapter.notifyDataSetChanged();
mPage++;
if (list == null || list.size() < LIMIT) {
mRvMedia.setFooterViewHide(true);
mRvMedia.setHasLoadMore(false);
} else {
mRvMedia.setFooterViewHide(false);
mRvMedia.setHasLoadMore(true);
}
if (mMediaBeanList.size() == 0) {
String mediaEmptyTils = ThemeUtils.resolveString(getContext(), R.attr.gallery_media_empty_tips, R.string.gallery_default_media_empty_tips);
EmptyViewUtils.showMessage(mLlEmptyView, mediaEmptyTils);
}
mRvMedia.onLoadMoreComplete();
}
@Override
public void onRequestBucketCallback(List<BucketBean> list) {
if (list == null || list.size() == 0) {
return;
}
mBucketBeanList.addAll(list);
mBucketAdapter.setSelectedBucket(list.get(0));
}
@Override
public void onItemClick(View view, int position) {
BucketBean bucketBean = mBucketBeanList.get(position);
String bucketId = bucketBean.getBucketId();
mRlBucektOverview.setVisibility(View.GONE);
if (TextUtils.equals(mBucketId, bucketId)) {
return;
}
mBucketId = bucketId;
EmptyViewUtils.showLoading(mLlEmptyView);
mRvMedia.setHasLoadMore(false);
mMediaBeanList.clear();
mMediaGridAdapter.notifyDataSetChanged();
mTvFolderName.setText(bucketBean.getBucketName());
mBucketAdapter.setSelectedBucket(bucketBean);
mRvMedia.setFooterViewHide(true);
mPage = 1;
mMediaGridPresenter.getMediaList(mBucketId, mPage, LIMIT);
}
@Override
public void onItemClick(RecyclerView.ViewHolder holder, int position) {
onObItemClick(position);
}
public void onObItemClick(int position) {
MediaBean mediaBean = mMediaBeanList.get(position);
if (mediaBean.getId() == Integer.MIN_VALUE) {
if (!CameraUtils.hasCamera(getContext())) {
Toast.makeText(getContext(), R.string.gallery_device_no_camera_tips, Toast.LENGTH_SHORT).show();
return;
}
boolean b = PermissionCheckUtils.checkCameraPermission(mMediaActivity, requestStorageAccessPermissionTips, MediaActivity.REQUEST_CAMERA_ACCESS_PERMISSION);
if (b) {
openCamera(mMediaActivity);
}
} else {
if (mConfiguration.isRadio()) {
if (mConfiguration.isImage()) {
radioNext(mediaBean);
} else {
videoRadioNext(mediaBean);
}
} else {
MediaBean firstBean = mMediaBeanList.get(0);
ArrayList<MediaBean> gridMediaList = new ArrayList<>();
gridMediaList.addAll(mMediaBeanList);
int pos = position;
if (firstBean.getId() == Integer.MIN_VALUE) {
pos = position - 1;
gridMediaList.clear();
List<MediaBean> list = mMediaBeanList.subList(1, mMediaBeanList.size());
gridMediaList.addAll(list);
}
RxBus.getDefault().post(new OpenMediaPageFragmentEvent(gridMediaList, pos));
}
}
}
/**
* 处理 Video 选择 是否预览
*
* @param mediaBean
*/
private void videoRadioNext(MediaBean mediaBean) {
if (!mConfiguration.isVideoPreview()) {
setPostMediaBean(mediaBean);
getActivity().finish();
return;
}
try {
Intent openVideo = new Intent(Intent.ACTION_VIEW);
openVideo.setDataAndType(Uri.parse(mediaBean.getOriginalPath()), "video/*");
startActivity(openVideo);
} catch (Exception e) {
Toast.makeText(getContext(), "启动播放器失败", Toast.LENGTH_SHORT).show();
}
}
/**
* 处理回调
*/
private void setPostMediaBean(MediaBean mediaBean) {
ImageCropBean bean = new ImageCropBean();
bean.copyMediaBean(mediaBean);
RxBus.getDefault().post(new ImageRadioResultEvent(bean));
}
/**
* 区分功能
*/
private void radioNext(MediaBean mediaBean) {
Logger.i("isCrop :" + mConfiguration.isCrop());
if (!mConfiguration.isCrop()) {
setPostMediaBean(mediaBean);
getActivity().finish();
} else {
//裁剪根据大家需求加上选择完图片后的回调
setPostMediaBean(mediaBean);
String originalPath = mediaBean.getOriginalPath();
File file = new File(originalPath);
Random random = new Random();
String outName = String.format(IMAGE_STORE_FILE_NAME, SimpleDateUtils.getNowTime() + "_" + random.nextInt(1024));
Logger.i("--->isCrop:" + mImageStoreCropDir);
Logger.i("--->mediaBean.getOriginalPath():" + mediaBean.getOriginalPath());
mCropPath = new File(mImageStoreCropDir, outName);
Uri outUri = Uri.fromFile(mCropPath);
if (!mImageStoreCropDir.exists()) {
mImageStoreCropDir.mkdirs();
}
if (!file.exists()) {
file.mkdirs();
}
Uri inputUri = Uri.fromFile(new File(mediaBean.getOriginalPath()));
Intent intent = new Intent(getContext(), UCropActivity.class);
// UCrop 参数 start
Bundle bundle = new Bundle();
bundle.putParcelable(UCrop.EXTRA_OUTPUT_URI, outUri);
bundle.putParcelable(UCrop.Options.EXTRA_ASPECT_RATIO_OPTIONS, mediaBean);
bundle.putInt(UCrop.Options.EXTRA_STATUS_BAR_COLOR, uCropStatusColor);
bundle.putInt(UCrop.Options.EXTRA_TOOL_BAR_COLOR, uCropToolbarColor);
bundle.putString(UCrop.Options.EXTRA_UCROP_TITLE_TEXT_TOOLBAR, uCropTitle);
bundle.putInt(UCrop.Options.EXTRA_UCROP_COLOR_WIDGET_ACTIVE, uCropActivityWidgetColor);
bundle.putInt(UCrop.Options.EXTRA_UCROP_WIDGET_COLOR_TOOLBAR, uCropToolbarWidgetColor);
bundle.putBoolean(UCrop.Options.EXTRA_HIDE_BOTTOM_CONTROLS, mConfiguration.isHideBottomControls());
bundle.putIntArray(UCrop.Options.EXTRA_ALLOWED_GESTURES, mConfiguration.getAllowedGestures());
bundle.putInt(UCrop.Options.EXTRA_COMPRESSION_QUALITY, mConfiguration.getCompressionQuality());
bundle.putInt(UCrop.Options.EXTRA_MAX_BITMAP_SIZE, mConfiguration.getMaxBitmapSize());
bundle.putFloat(UCrop.Options.EXTRA_MAX_SCALE_MULTIPLIER, mConfiguration.getMaxScaleMultiplier());
bundle.putFloat(UCrop.EXTRA_ASPECT_RATIO_X, mConfiguration.getAspectRatioX());
bundle.putFloat(UCrop.EXTRA_ASPECT_RATIO_Y, mConfiguration.getAspectRatioY());
bundle.putInt(UCrop.EXTRA_MAX_SIZE_X, mConfiguration.getMaxResultWidth());
bundle.putInt(UCrop.EXTRA_MAX_SIZE_Y, mConfiguration.getMaxResultHeight());
bundle.putInt(UCrop.Options.EXTRA_ASPECT_RATIO_SELECTED_BY_DEFAULT, mConfiguration.getSelectedByDefault());
bundle.putBoolean(UCrop.Options.EXTRA_FREE_STYLE_CROP, mConfiguration.isFreestyleCropEnabled());
bundle.putParcelable(UCrop.EXTRA_INPUT_URI, inputUri);
// UCrop 参数 end
int bk = FileUtils.existImageDir(inputUri.getPath());
Logger.i("--->" + inputUri.getPath());
Logger.i("--->" + outUri.getPath());
ArrayList<AspectRatio> aspectRatioList = new ArrayList<>();
AspectRatio[] aspectRatios = mConfiguration.getAspectRatio();
if (aspectRatios != null) {
for (int i = 0; i < aspectRatios.length; i++) {
aspectRatioList.add(i, aspectRatios[i]);
Logger.i("自定义比例=>" + aspectRatioList.get(i).getAspectRatioX() + " " + aspectRatioList.get(i).getAspectRatioY());
}
}
// AspectRatio[]aspectRatios = mConfiguration.getAspectRatio();
bundle.putParcelableArrayList(UCrop.Options.EXTRA_ASPECT_RATIO_OPTIONS, aspectRatioList);//EXTRA_CONFIGURATION
intent.putExtras(bundle);
if (bk != -1) {
//裁剪
startActivityForResult(intent, CROP_IMAGE_REQUEST_CODE);
} else {
Logger.w("点击图片无效");
}
}
}
public void openCamera(Context context) {
boolean image = mConfiguration.isImage();
Intent captureIntent = image ? new Intent(MediaStore.ACTION_IMAGE_CAPTURE) : new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (captureIntent.resolveActivity(context.getPackageManager()) == null) {
Toast.makeText(getContext(), R.string.gallery_device_camera_unable, Toast.LENGTH_SHORT).show();
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
String filename = String.format(image ? IMAGE_STORE_FILE_NAME : VIDEO_STORE_FILE_NAME, dateFormat.format(new Date()));
Logger.i("openCamera:" + mImageStoreDir.getAbsolutePath());
File fileImagePath = new File(mImageStoreDir, filename);
mImagePath = fileImagePath.getAbsolutePath();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileImagePath));
} else {
ContentValues contentValues = new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA, mImagePath);
Uri uri = getContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
// video : 1: 高质量 0 低质量
// captureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(captureIntent, TAKE_IMAGE_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Logger.i("onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode);
if (requestCode == TAKE_IMAGE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Logger.i(String.format("拍照成功,图片存储路径:%s", mImagePath));
mMediaScanner.scanFile(mImagePath, mConfiguration.isImage() ? IMAGE_TYPE : "", this);
} else if (requestCode == 222) {
Toast.makeText(getActivity(), "摄像成功", Toast.LENGTH_SHORT).show();
} else if (requestCode == CROP_IMAGE_REQUEST_CODE && data != null) {
Logger.i("裁剪成功");
refreshUI();
onCropFinished();
}
}
/**
* 裁剪之后
* setResult(RESULT_OK, new Intent()
* .putExtra(UCrop.EXTRA_OUTPUT_URI, uri)
* .putExtra(UCrop.EXTRA_OUTPUT_CROP_ASPECT_RATIO, resultAspectRatio)
* .putExtra(UCrop.EXTRA_OUTPUT_IMAGE_WIDTH, imageWidth)
* .putExtra(UCrop.EXTRA_OUTPUT_IMAGE_HEIGHT, imageHeight)
*/
private void onCropFinished() {
if (iListenerRadio != null && mCropPath != null) {
if (mConfiguration.isCrop()) {
iListenerRadio.cropAfter(mCropPath);
}
} else {
Logger.i("# CropPath is null!# ");
}
//裁剪默认会关掉这个界面. 实现接口返回true则不关闭.
if (iListenerRadio == null) {
getActivity().finish();
} else {
boolean flag = iListenerRadio.isActivityFinish();
Logger.i("# crop image is flag # :" + flag);
if (flag)
getActivity().finish();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (!TextUtils.isEmpty(mImagePath)) {
outState.putString(TAKE_URL_STORAGE_KEY, mImagePath);
}
if (!TextUtils.isEmpty(mBucketId)) {
outState.putString(BUCKET_ID_KEY, mBucketId);
}
}
@Override
protected void onRestoreState(Bundle savedInstanceState) {
}
//****************************************************************************
@Override
protected void onSaveState(Bundle outState) {
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState == null) {
return;
}
mImagePath = savedInstanceState.getString(TAKE_URL_STORAGE_KEY);
mBucketId = savedInstanceState.getString(BUCKET_ID_KEY);
}
@Override
public void onDestroy() {
super.onDestroy();
mMediaScanner.unScanFile();
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.tv_preview) {
RxBus.getDefault().post(new OpenMediaPreviewFragmentEvent());
} else if (id == R.id.tv_folder_name) {
v.setEnabled(false);
if (isShowRvBucketView()) {
hideRvBucketView();
} else {
showRvBucketView();
}
}
}
public boolean isShowRvBucketView() {
return mRlBucektOverview != null && mRlBucektOverview.getVisibility() == View.VISIBLE;
}
public void showRvBucketView() {
if (mRlBucektOverview == null) {
slideInUnderneathAnimation = new SlideInUnderneathAnimation(mRlBucektOverview);
}
mRlBucektOverview.setVisibility(View.VISIBLE);
slideInUnderneathAnimation
.setDirection(Animation.DIRECTION_DOWN)
.setDuration(Animation.DURATION_DEFAULT)
.setListener(animation -> mTvFolderName.setEnabled(true))
.animate();
}
public void hideRvBucketView() {
if (slideOutUnderneathAnimation == null) {
slideOutUnderneathAnimation = new SlideOutUnderneathAnimation(mRvBucket);
}
slideOutUnderneathAnimation
.setDirection(Animation.DIRECTION_DOWN)
.setDuration(Animation.DURATION_DEFAULT)
.setListener(animation -> {
mTvFolderName.setEnabled(true);
mRlBucektOverview.setVisibility(View.GONE);
})
.animate();
}
/**
* Observable刷新图库
*/
public void refreshUI() {
try {
Logger.i("->getImageStoreDirByFile().getPath().toString():" + getImageStoreDirByFile().getPath());
Logger.i("->getImageStoreCropDirByStr ().toString():" + getImageStoreCropDirByStr());
if (!TextUtils.isEmpty(mImagePath))
mMediaScanner.scanFile(mImagePath, IMAGE_TYPE, this);
if (mCropPath != null) {
Logger.i("->mCropPath:" + mCropPath.getPath() + " " + IMAGE_TYPE);
mMediaScanner.scanFile(mCropPath.getPath(), IMAGE_TYPE, this);
}
} catch (Exception e) {
Logger.e(e.getMessage());
}
}
@Override
public void onScanCompleted(String[] images) {
if (images == null || images.length == 0) {
Logger.i("images empty");
return;
}
// mediaBean 有可能为Null,onNext 做了处理,在 getMediaBeanWithImage 时候就不处理Null了
Observable.create((ObservableOnSubscribe<MediaBean>) subscriber -> {
MediaBean mediaBean =
mConfiguration.isImage() ? MediaUtils.getMediaBeanWithImage(getContext(), images[0])
:
MediaUtils.getMediaBeanWithVideo(getContext(), images[0]);
subscriber.onNext(mediaBean);
subscriber.onComplete();
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableObserver<MediaBean>() {
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
Logger.i("获取MediaBean异常" + e.toString());
}
@Override
public void onNext(MediaBean mediaBean) {
if (!isDetached() && mediaBean != null) {
int bk = FileUtils.existImageDir(mediaBean.getOriginalPath());
if (bk != -1) {
mMediaBeanList.add(1, mediaBean);
mMediaGridAdapter.notifyDataSetChanged();
if (mConfiguration.isTakePicCrop()) {
onObItemClick(1);
}
} else {
Logger.i("获取:无");
}
}
}
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
RxBus.getDefault().remove(mMediaCheckChangeDisposable);
RxBus.getDefault().remove(mCloseMediaViewPageFragmentDisposable);
}
/**
* onAttach 转 onStart
*/
public void onLoadFile() {
//没有的话就默认路径
if (getImageStoreDirByFile() == null && getImageStoreDirByStr() == null) {
mImageStoreDir = new File(Environment.getExternalStorageDirectory(), "/DCIM/IMMQY/");
setImageStoreCropDir(mImageStoreDir);
}
if (!mImageStoreDir.exists()) {
mImageStoreDir.mkdirs();
}
if (getImageStoreCropDirByFile() == null && getImageStoreCropDirByStr() == null) {
mImageStoreCropDir = new File(mImageStoreDir, "crop");
if (!mImageStoreCropDir.exists()) {
mImageStoreCropDir.mkdirs();
}
setImageStoreCropDir(mImageStoreCropDir);
}
}
}