Android拍照调用系统相册仿微信封装总结,治疗各种崩溃,图片横竖问题压缩等问题。

项目下载地址:https://github.com/Aiushtha/android-PictureSelector

最早使用android调用系统拍照然后遇到很多空指针等问题以及各种android 不同版本Intent取data有时候会空指针之类的api兼容问题,像使用红米note在开了很多应用后,再启动拍照系统,会发生拍照崩溃图片丢失等问题,用微信控件有时拍照有极小概率拍照无效等等奇怪的问题,其原因是因为Activity被回收了,变量变成null,

还有三星手机可能会遇到变量空针

需要在AndroidManifest.xml的Activity里加入

?
1
<strong>android</strong><strong>:configChanges=</strong><strong> "mcc|mnc|keyboard|keyboardHidden|navigation|orientation|screenSize|fontScale" </strong>

 

如何在Fragment上调用拍照,图片如何压缩

经过一段时间优化,修复了一些坑。我觉得目前的代码比较可靠,总结一下封装后分享出来。

\

\

 

\

\

 

?
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
package com.cn.demo.takephoto;
 
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
import java.io.File;
import java.text.DecimalFormat;
import java.util.List;
 
import me.lxz.photopicker.camera.PhotoPickManger;
import me.lxz.photopicker.tools.SimpleImageLoader;
 
public class SimpleDemoActivity extends AppCompatActivity {
 
     PhotoPickManger pickManger;
     private View btn;
     private ImageView img;
     private TextView tv;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         /**图片加载器*/
         SimpleImageLoader.init( this .getApplicationContext());
 
 
         btn = findViewById(R.id.btn);
         img = (ImageView) findViewById(R.id.img);
         tv=(TextView)findViewById(R.id.tv);
 
         pickManger = new PhotoPickManger( "pick" , this , savedInstanceState, new PhotoPickManger.OnPhotoPickFinsh() {
             @Override
             public void onPhotoPick(List<file> list) {
                 tv.setText( "" );
                 Toast.makeText(getApplicationContext(), "path:" + list.get( 0 ).getPath() + " length:" + list.get( 0 ).length(), Toast.LENGTH_SHORT).show();
                 tv.append( "path:" + list.get( 0 ).getPath());
                 tv.append( "\nlength:" + new DecimalFormat( "#.##" ).format(( 1 .0d*list.get( 0 ).length()/ 1024 / 1024 ))+ "MB" );
                 /**是否图片压缩*/
                 processImg();
 
             }
         });
         /**是否在*/
         pickManger.setCut( false );
         pickManger.flushBundle();
 
         btn.setOnClickListener( new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 pickManger.clearCache();
                 pickManger.start(PhotoPickManger.Mode.SYSTEM_CAMERA);
             }
         });
 
 
     }
     /**图片压缩*/
     private void processImg() {
         pickManger.doProcessedPhotos( new PhotoPickManger.OnProcessedPhotos() {
             @Override
             public void onProcessed(List<file> list) {
                 SimpleImageLoader.displayImage(list.get( 0 ), img);
                 tv.append( "\nprogress length:" + new DecimalFormat( "#.##" ).format(( 1 .0d*list.get( 0 ).length()/ 1024 / 1024 ))+ "MB" );
            }
         });
     }
 
 
 
     @Override
     protected void onActivityResult( int requestCode, int resultCode, Intent data) {
         super .onActivityResult(requestCode, resultCode, data);
         pickManger.onActivityResult(requestCode, resultCode, data);
     }
 
     @Override
     public void onSaveInstanceState(Bundle savedInstanceState) {
         super .onSaveInstanceState(savedInstanceState);
         pickManger.onSaveInstanceState(savedInstanceState);
     }
}</file></file>

核心代码:

 

 

?
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
package me.lxz.photopicker.camera;
 
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
 
import me.iwf.photopicker.PhotoPickerActivity;
import me.iwf.photopicker.utils.PhotoPickerIntent;
import me.lxz.photopicker.tools.PictureUtil;
 
 
/**
  * 图片选择器
  */
public final class PhotoPickManger {
 
     public interface OnProcessedPhotos {
         void onProcessed(List<file> list);
     }
 
     public interface OnPhotoPickFinsh {
         public void onPhotoPick(List<file> list);
     }
 
 
     /**
      * 模式
      */
     public enum Mode {
         /**
          * 系统相机
          */ SYSTEM_CAMERA,
         /**
          * 系统图库
          */ SYSTEM_IMGCAPTRUE,
         /**
          * 类似微信图库
          */ AS_WEIXIN_IMGCAPTRUE
     }
 
 
     /**
      * 用于区别哪一个图片选择器
      */
     private static String currentPickMangerName;
 
     /**
      * 测试用
      */
     private boolean isDebugToast = true ;
 
     /**
      * 用于区别其他图片选择器
      */
     private String name;
 
 
     public final String SAVE_STATIC_NAME = "save_currentPickMangerName" ;
 
     /**
      * 字段保存所已选择的图片
      */
     public final String SAVE_SELECTED_PHOTOS = "save_selected_photos" ;
 
     /**
      * 字段保存所拍照已选择的图片
      */
     public final String SAVE_CACHE_CAMERA = "save_cache_camera" ;
 
     /**
      * 字段保存所拍照是否选择裁剪
      */
     public final String SAVE_CACHE_IS_CUT = "save_cache_is_cut" ;
 
 
     /**
      * 字段保存待处理图片
      */
     public final String SAVE_CACHE_CUT_QUEUE = "save_cache_cut_queue" ;
 
 
     /**
      * 是否裁剪 只对系统相机和系统相册有效
      */
     private boolean isCut = false ;
     /**
      * 是否缩略
      */
     private boolean isOptimize = false ;
 
     /**
      * 设置返回最大图片数 对系统相机和相册调用无效
      * 默认1
      */
     private int returnFileCount = 1 ;
 
 
     private OnPhotoPickFinsh onPhotoPickFinsh;
 
     private File tempFile;
 
     private Handler handler = new Handler();
 
     /**
      * 已经选择的拍照图片
      */
     public ArrayList<file> selectsPhotos = new ArrayList<>();
 
 
     private Activity activity;
 
     /**
      * 用于缓存
      */
     public Bundle bundle;
     /**
      * 系统拍照标示code
      */
     private final int PHOTO_REQUEST_TAKEPHOTO = 1 ; // 拍照
     /**
      * 系统相册标示code
      */
     private final int PHOTO_REQUEST_GALLERY = 2 ; // 从相册中选择
     /**
      * 系统照片返回标示code
      */
     private final int PHOTO_REQUEST_CUT = 3 ; // 结果
     /**
      * 仿微信相册返回code
      */
     public final static int AS_WEIXIN_REQUEST_CODE = 4 ;
 
     /**
      * 如果多图待剪切则保存到该队列里
      */
     public ArrayList<file> willCutOfFileQueue = new ArrayList<>();
 
 
     /**
      * 图片缓存地址
      */
     public String cacheFilePath = "/img/" ;
 
     /**
      * 默认裁剪大小
      */
     public int defaultCutSize = 150 ;
 
     /**
      * 至少大于多少的图片进行处理
      */
     public int needProcessFileLength = ( int ) 0.5 * 1024 * 1024 ;
 
     /***
      * 构造方法
      *
      * @param name             为图片选择器默认一个别名 用来区别那一个选择器被选择了
      * @param activity
      * @param bundle           当系统内存不足时,重建时取出变量
      * @param onPhotoPickFinsh 图片选择成功时回调
      */
     public PhotoPickManger(String name, Activity activity, Bundle bundle, OnPhotoPickFinsh onPhotoPickFinsh) {
         this .onPhotoPickFinsh = onPhotoPickFinsh;
         this .name = name;
         this .activity = activity;
         this .bundle = bundle;
 
         if (bundle != null ) {
             isCut = bundle.getBoolean(SAVE_CACHE_IS_CUT + "_" + name);
             currentPickMangerName = bundle.getString(SAVE_STATIC_NAME);
             willCutOfFileQueue = (ArrayList<file>) bundle.getSerializable(SAVE_CACHE_CUT_QUEUE + "name" );
         }
     }
 
     /**
      * 处理掉重建时的缓存
      */
     public void flushBundle() {
         if (bundle != null ) {
             if (isDebugToast) {
                 Toast.makeText(activity, "bundle is refresh" , Toast.LENGTH_LONG).show();
             }
             selectsPhotos = (ArrayList<file>) bundle.getSerializable(SAVE_SELECTED_PHOTOS + "_" + name);
             if (selectsPhotos == null ) {
                 selectsPhotos = new ArrayList<>();
             }
             tempFile = (File) bundle.getSerializable(SAVE_CACHE_CAMERA + "_" + name);
             if (tempFile != null ) {
                 if (tempFile.exists()) {
                     if (tempFile.length() > 0 ) {
                         if (!isCut) {
                             selectsPhotos.add(tempFile);
                         } else {
                             startPhotoZoom(Uri.fromFile(tempFile), defaultCutSize);
                         }
                         tempFile = null ;
                     } else {
                         tempFile.delete();
                         tempFile = null ;
                     }
 
                 }
             }
             bundle.remove(SAVE_CACHE_CAMERA + "_" + name);
             if (!selectsPhotos.isEmpty()) {
                 if (onPhotoPickFinsh != null ) onPhotoPickFinsh.onPhotoPick(selectsPhotos);
             }
 
         }
     }
 
     /**
      * 保存变量
      */
     public void onSaveInstanceState(Bundle savedInstanceState) {
         this .bundle = savedInstanceState;
         if (selectsPhotos != null && !selectsPhotos.isEmpty()) {
             savedInstanceState.putSerializable(SAVE_SELECTED_PHOTOS + "_" + name, selectsPhotos);
         }
         if (tempFile != null ) {
             savedInstanceState.putSerializable(SAVE_CACHE_CAMERA + "_" + name, tempFile);
         }
         savedInstanceState.putBoolean(SAVE_CACHE_IS_CUT + "_" + name, isCut);
         savedInstanceState.putSerializable(SAVE_CACHE_CUT_QUEUE + "_" + name, willCutOfFileQueue);
         savedInstanceState.putSerializable(SAVE_STATIC_NAME, currentPickMangerName);
     }
 
     /**
      * 如果是单一拍照,在拍照前应该清理缓存
      */
     public void clearCache() {
         getSelectsPhotos().clear();
         tempFile = null ;
         if (bundle != null ) {
             bundle.remove(SAVE_SELECTED_PHOTOS + "_" + name);
             bundle.remove(SAVE_CACHE_CAMERA + "_" + name);
         }
 
 
     }
 
     /**
      * 生成一个临时的缓存文件
      */
     private File getFile() {
         File dir = new File(Environment.getExternalStorageDirectory().getPath()
                 + cacheFilePath);
         if (!dir.exists()) {
             dir.mkdirs();
         }
         File file = new File(Environment.getExternalStorageDirectory()
                 .getPath() + cacheFilePath, getPhotoFileName());
         return file;
     }
 
     // 使用系统当前日期加以调整作为照片的名称
     private String getPhotoFileName() {
         Date date = new Date(System.currentTimeMillis());
         SimpleDateFormat dateFormat = new SimpleDateFormat(
                 "'IMG'_yyyyMMdd_HHmmss" );
         return dateFormat.format(date) + ".jpg" ;
     }
 
     /**
      * // 调用系统的拍照功能
      */
     private void startCamearPicCut() {
         tempFile = getFile();
         Log.d( "test" , "start:" + tempFile.exists() + " " + tempFile.length());
         // this.isCutOut = b;
         // 调用系统的拍照功能
         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
         intent.putExtra( "camerasensortype" , 2 ); // 调用前置摄像头
         intent.putExtra( "autofocus" , true ); // 自动对焦
         intent.putExtra( "fullScreen" , false ); // 全屏
         intent.putExtra( "showActionIcons" , false );
         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
         activity.startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
     }
 
     /**
      * 调用系统的相册
      */
     private void startImageCaptrue() {
         tempFile = getFile();
         Intent intent = new Intent(Intent.ACTION_PICK, null );
         intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                 "image/*" );
         activity.startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
     }
 
 
     /**
      * 调用仿微信图库
      */
 
     private void startAsWeixinImageCaptrue() {
         PhotoPickerIntent intent = new PhotoPickerIntent(activity);
         intent.setPhotoCount(returnFileCount);
         activity.startActivityForResult(intent, AS_WEIXIN_REQUEST_CODE);
     }
 
     /**
      * 启动
      */
 
     public void start(Mode mode) {
         currentPickMangerName = this .name;
         switch (mode) {
             case SYSTEM_CAMERA:
                 startCamearPicCut();
                 break ;
             case SYSTEM_IMGCAPTRUE:
                 startImageCaptrue();
                 break ;
             case AS_WEIXIN_IMGCAPTRUE:
                 startAsWeixinImageCaptrue();
                 break ;
         }
     }
 
 
     /**
      * 回调onActivityResult事件
      */
     @SuppressWarnings ( "unused" )
     public void onActivityResult( final int requestCode, final int resultCode, final Intent data) {
         if (currentPickMangerName == null ) {
             if (bundle != null ) {
                 currentPickMangerName = bundle.getString(SAVE_STATIC_NAME);
                 if (!currentPickMangerName.equals(name)) return ;
             }
         } else {
             if (!currentPickMangerName.equals(name)) return ;
         }
         try {
             switch (requestCode) {
                 case PHOTO_REQUEST_TAKEPHOTO:
                     if (isCut) {
                         startPhotoZoom(Uri.fromFile(tempFile), defaultCutSize); // 裁剪
                     } else {
                         if (tempFile.length() == 0 ) {
                             tempFile.delete();
                         } else {
                             finish(tempFile);
                         }
                     }
                     return ;
                 case PHOTO_REQUEST_GALLERY:
                     if (isCut) {
                         if (data != null ) {
                             startPhotoZoom(data.getData(), defaultCutSize);
                         }
                     } else {
 
                         File file = null ;
                         try {
                             String path = getRealPathFromURI(data.getData());
                             file = new File(path);
 
                             if (file == null ) return ;
                             if (file.length() == 0 ) {
                                 file.delete();
                                 return ;
                             }
                             finish(file);
                         } catch (Exception e) {
                             // TODO Auto-generated catch block
                             e.printStackTrace();
                         }
 
                     }
                     return ;
 
                 case PHOTO_REQUEST_CUT:
                     if (data != null ) {
                         setCutPicToView(data);
                     } else {
                         flushCutPhotos();
                     }
                     return ;
                 case AS_WEIXIN_REQUEST_CODE:
                     List<string> photos = null ;
                     try {
                         if (requestCode == AS_WEIXIN_REQUEST_CODE) {
                             if (data != null ) {
                                 photos = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
                                 if (!isCut) {
 
                                     List<file> list = new ArrayList<file>();
                                     if (photos != null && !photos.isEmpty()) {
                                         for (String str : photos) {
                                             list.add( new File(str));
                                         }
                                     } else {
                                         return ;
                                     }
                                     finish(list);
                                 } else {
                                     if (photos != null && !photos.isEmpty()) {
                                         /**如果只有一张照片可以直接裁剪 否则*/
                                         if (photos.size() == 1 ) {
                                             for (String str : photos) {
                                                 tempFile = getFile();
                                                 copyFile(str, tempFile.getAbsolutePath());
                                                 startPhotoZoom(Uri.fromFile(tempFile), defaultCutSize); // 裁剪
                                             }
                                         } else {
                                             for (String str : photos) {
                                                 willCutOfFileQueue.add( new File(str));
                                             }
                                             tempFile = getFile();
                                             copyFile(willCutOfFileQueue.get( 0 ).getAbsolutePath(), tempFile.getAbsolutePath());
                                             startPhotoZoom(Uri.fromFile(tempFile), defaultCutSize); // 裁剪
                                             willCutOfFileQueue.remove( 0 );
                                         }
                                     }
 
                                 }
                             }
 
                         }
                     } catch (Exception e) {
                         e.printStackTrace();
                     }
 
                     return ;
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 
 
     /**
      * 如果多图处理裁剪队列
      */
     private void flushCutPhotos() {
         if (willCutOfFileQueue != null && !willCutOfFileQueue.isEmpty()) {
             tempFile = getFile();
             copyFile(willCutOfFileQueue.get( 0 ).getAbsolutePath(), tempFile.getAbsolutePath());
             willCutOfFileQueue.remove( 0 );
             startPhotoZoom(Uri.fromFile(tempFile), defaultCutSize); // 裁剪
         }
     }
 
     /**
      * 文件复制
      */
     private void copyFile(String oldPath, String newPath) {
         try {
             int bytesum = 0 ;
             int byteread = 0 ;
             File oldfile = new File(oldPath);
             if (oldfile.exists()) {                  //文件存在时
                 InputStream inStream = new FileInputStream(oldPath);      //读入原文件
                 FileOutputStream fs = new FileOutputStream(newPath);
                 byte [] buffer = new byte [ 1444 ];
                 int length;
                 while ((byteread = inStream.read(buffer)) != - 1 ) {
                     bytesum += byteread;            //字节数 文件大小
                     System.out.println(bytesum);
                     fs.write(buffer, 0 , byteread);
                 }
                 inStream.close();
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 
     /**
      * 对当前已选图片进行压缩
      */
     public void doProcessedPhotos( final OnProcessedPhotos on) {
 
         if (getSelectsPhotos() != null && !getSelectsPhotos().isEmpty()) {
             new Thread( new Runnable() {
                 @Override
                 public void run() {
                     for (Iterator<file> it = getSelectsPhotos().iterator(); it.hasNext(); ) {
                         try {
                             File file = it.next();
                             if (file.length() > needProcessFileLength) {
                                 final Bitmap bm = PictureUtil.getSmallBitmap(file.getAbsolutePath(), 720 , 1200 );
                                 try {
                                     FileOutputStream fos = new FileOutputStream(file);
                                     bm.compress(Bitmap.CompressFormat.JPEG, 95 , fos);
                                 } catch (FileNotFoundException e) {
                                     e.printStackTrace();
                                 }
                             }
                         } catch (Exception e) {
                             e.printStackTrace();
                         }
                     }
                     handler.post( new Runnable() {
                         @Override
                         public void run() {
                             on.onProcessed(getSelectsPhotos());
                         }
                     });
 
 
                 }
             }).start();
 
         }
     }
 
     /**
      * 图片选择完成并回调
      */
     private void finish( final File file) {
         finish(createFiles(file));
     }
 
     /**
      * 图片选择完成并回调
      */
     private void finish( final List<file> files) {
         new Thread( new Runnable() {
             @Override
             public void run() {
                 for (Iterator<file> it = files.iterator(); it.hasNext(); ) {
                     File file = it.next();
                     if (!file.exists() || file.length() == 0 ) {
                         it.remove();
                     } else {
                         changFile(file.getPath());
                     }
                 }
                 handler.post( new Runnable() {
                     @Override
                     public void run() {
                         selectsPhotos.addAll(files);
                         if (!files.isEmpty()) {
                             if (onPhotoPickFinsh != null ) onPhotoPickFinsh.onPhotoPick(files);
                         }
                         tempFile = null ;
                         flushCutPhotos();
                     }
                 });
             }
         }).start();
 
 
     }
 
     /**
      * 三星手机将横向图片转换为竖向
      */
     public void changFile(String file) {
         BitmapFactory.Options options = new BitmapFactory.Options();
 
         /**
          * 最关键在此,把options.inJustDecodeBounds = true;
          * 这里再decodeFile(),返回的bitmap为空,但此时调用options.outHeight时,已经包含了图片的高了
          */
         options.inJustDecodeBounds = true ;
         BitmapFactory.decodeFile(file, options);
         int width = options.outWidth;
         int height = options.outHeight;
         if (width > height) {
 
             Bitmap bit = bitmapFromFile(file, width, height);
             bit = adjustPhotoRotation(bit, 1 );
             try {
                 bit.compress(Bitmap.CompressFormat.PNG, 100 , new FileOutputStream(file));
             } catch (FileNotFoundException e) {
                 e.printStackTrace();
             }
         }
     }
 
     /**
      * 旋转图片
      */
     public Bitmap adjustPhotoRotation(Bitmap bm, int count) {
         int orientationDegree = 90 ;
         Matrix m = new Matrix();
         for ( int i = 0 ; i < count; i++) {
             m.setRotate(orientationDegree, bm.getWidth(),
                     bm.getHeight());
             float targetX, targetY;
             if (orientationDegree == 90 ) {
                 targetX = bm.getHeight();
                 targetY = 0 ;
             } else {
                 targetX = bm.getHeight();
                 targetY = bm.getWidth();
             }
 
             final float [] values = new float [ 9 ];
             m.getValues(values);
 
             float x1 = values[Matrix.MTRANS_X];
             float y1 = values[Matrix.MTRANS_Y];
 
             m.postTranslate(targetX - x1, targetY - y1);
         }
 
         Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(),
                 Bitmap.Config.ARGB_8888);
         Paint paint = new Paint();
         Canvas canvas = new Canvas(bm1);
         canvas.drawBitmap(bm, m, paint);
 
         // ?????bitmap????
         bm.recycle();
 
         return bm1;
     }
 
     private List<file> createFiles(File file) {
         List<file> list = new ArrayList<>();
         list.add(file);
         return list;
     }
 
     /**
      * 根据Uri获得File文件路径
      */
     public String getRealPathFromURI(Uri contentUri) {
         String res = null ;
         String[] proj = {MediaStore.Images.Media.DATA};
         Cursor cursor = activity.getContentResolver().query(contentUri, proj,
                 null , null , null );
         if (cursor.moveToFirst()) {
             ;
             int column_index = cursor
                     .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
             res = cursor.getString(column_index);
         }
         cursor.close();
         return res;
     }
 
     /**
      * 启动裁剪
      */
     private void startPhotoZoom(Uri uri, int size) {
         Log.d( "test" , uri.toString());
         Intent intent = new Intent( "com.android.camera.action.CROP" );
         intent.setDataAndType(uri, "image/*" );
         // crop为true是设置在开启的intent中设置显示的view可以剪裁
         intent.putExtra( "crop" , "true" );
 
         // aspectX aspectY 是宽高的比例
         intent.putExtra( "aspectX" , 1 );
         intent.putExtra( "aspectY" , 1 );
 
         // outputX,outputY 是剪裁图片的宽高
         intent.putExtra( "outputX" , size);
         intent.putExtra( "outputY" , size);
 
         intent.putExtra( "return-data" , true );
         intent.putExtra( "noFaceDetection" , true );
         activity.startActivityForResult(intent, PHOTO_REQUEST_CUT);
     }
 
     // 将进行剪裁后的图片显示到UI界面上
     private void setCutPicToView(Intent picdata) {
         Bundle bundle = picdata.getExtras();
         if (bundle != null ) {
             Bitmap photo = bundle.getParcelable( "data" );
             if (photo != null ) {
                 FileOutputStream fOut = null ;
                 try {
                     fOut = new FileOutputStream(tempFile);
                 } catch (FileNotFoundException e) {
                     e.printStackTrace();
                 }
                 photo.compress(Bitmap.CompressFormat.JPEG, 100 , fOut);
             }
             finish(tempFile);
 
         }
     }
 
 
     /**
      * 获取一个指定大小的bitmap
      *
      * @param reqWidth  目标宽度
      * @param reqHeight 目标高度
      */
     public Bitmap bitmapFromFile(String pathName, int reqWidth,
                                  int reqHeight) {
         if (reqHeight == 0 || reqWidth == 0 ) {
             return BitmapFactory.decodeFile(pathName);
         } else {
             BitmapFactory.Options options = new BitmapFactory.Options();
             options.inJustDecodeBounds = true ;
             BitmapFactory.decodeFile(pathName, options);
 
             options = calculateInSampleSize(options, reqWidth,
                     reqHeight);
             return BitmapFactory.decodeFile(pathName, options);
         }
     }
 
     /**
      * 图片压缩处理(使用Options的方法)
      *<p>
      *
 
      * <b>说明</b> 使用方法:
      * 首先你要将Options的inJustDecodeBounds属性设置为true,BitmapFactory.decode一次图片 。
      * 然后将Options连同期望的宽度和高度一起传递到到本方法中。
      * 之后再使用本方法的返回值做参数调用BitmapFactory.decode创建图片。
      *</p><p>
      *
 
      * <b>说明</b> BitmapFactory创建bitmap会尝试为已经构建的bitmap分配内存
      * ,这时就会很容易导致OOM出现。为此每一种创建方法都提供了一个可选的Options参数
      * ,将这个参数的inJustDecodeBounds属性设置为true就可以让解析方法禁止为bitmap分配内存
      * ,返回值也不再是一个Bitmap对象, 而是null。虽然Bitmap是null了,但是Options的outWidth、
      * outHeight和outMimeType属性都会被赋值。
      *
      * @param reqWidth  目标宽度,这里的宽高只是阀值,实际显示的图片将小于等于这个值
      * @param reqHeight 目标高度,这里的宽高只是阀值,实际显示的图片将小于等于这个值
      */
     public BitmapFactory.Options calculateInSampleSize(
             final BitmapFactory.Options options, final int reqWidth,
             final int reqHeight) {
         // 源图片的高度和宽度
         final int height = options.outHeight;
         final int width = options.outWidth;
         int inSampleSize = 1 ;
         if (height > reqHeight || width > reqWidth) {
             // 计算出实际宽高和目标宽高的比率
             final int heightRatio = Math.round(( float ) height
                     / ( float ) reqHeight);
             final int widthRatio = Math.round(( float ) width
                     / ( float ) reqWidth);
             // 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高
             // 一定都会大于等于目标的宽和高。
             inSampleSize = heightRatio < widthRatio ? heightRatio
                     : widthRatio;
         }
         // 设置压缩比例
         options.inSampleSize = inSampleSize;
         options.inJustDecodeBounds = false ;
         return options;
     }
 
     public Activity getActivity() {
         return activity;
     }
 
     public PhotoPickManger setActivity(Activity activity) {
         this .activity = activity;
         return this ;
     }
 
 
     public boolean isCut() {
         return isCut;
     }
 
     public PhotoPickManger setIsCut( boolean isCut) {
         this .isCut = isCut;
         return this ;
     }
 
     public boolean isOptimize() {
         return isOptimize;
     }
 
     public PhotoPickManger setIsOptimize( boolean isOptimize) {
         this .isOptimize = isOptimize;
         return this ;
     }
 
     public int getReturnFileCount() {
         return returnFileCount;
     }
 
     public PhotoPickManger setReturnFileCount( int returnFileCount) {
         this .returnFileCount = returnFileCount;
         return this ;
     }
 
     public OnPhotoPickFinsh getOnPhotoPickFinsh() {
         return onPhotoPickFinsh;
     }
 
     public PhotoPickManger setOnPhotoPickFinsh(OnPhotoPickFinsh onPhotoPickFinsh) {
         this .onPhotoPickFinsh = onPhotoPickFinsh;
         return this ;
     }
 
     public boolean isDebugToast() {
         return isDebugToast;
     }
 
     public PhotoPickManger setDebugToast( boolean isDebugToast) {
         this .isDebugToast = isDebugToast;
         return this ;
     }
 
     public void setCut( boolean isCut) {
         this .isCut = isCut;
     }
 
     public Bundle getBundle() {
         return bundle;
     }
 
     public PhotoPickManger setBundle(Bundle bundle) {
         this .bundle = bundle;
         return this ;
     }
 
     public File getTempFile() {
         return tempFile;
     }
 
     public ArrayList<file> getSelectsPhotos() {
         return selectsPhotos;
     }
 
     public int getNeedProcessFileLength() {
         return needProcessFileLength;
     }
 
     public PhotoPickManger setNeedProcessFileLength( int needProcessFileLength) {
         this .needProcessFileLength = needProcessFileLength;
         return this ;
     }
 
     public static String getCurrentPickMangerName() {
         return currentPickMangerName;
     }
 
     public static void setCurrentPickMangerName(String currentPickMangerName) {
         PhotoPickManger.currentPickMangerName = currentPickMangerName;
     }
 
     public String getCacheFilePath() {
         return cacheFilePath;
     }
 
     public PhotoPickManger setCacheFilePath(String cacheFilePath) {
         this .cacheFilePath = cacheFilePath;
         return this ;
     }
 
     public String getName() {
         return name;
     }
 
     public PhotoPickManger setName(String name) {
         this .name = name;
         return this ;
     }
 
     public PhotoPickManger setTempFile(File tempFile) {
         this .tempFile = tempFile;
         return this ;
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值