1
+ package com .jingewenku .abrahamcaijin .commonutil ;
2
+
3
+ import android .app .Activity ;
4
+ import android .content .Intent ;
5
+ import android .graphics .Bitmap ;
6
+ import android .net .Uri ;
7
+ import android .provider .MediaStore ;
8
+
9
+ /**
10
+ * @Description:主要功能:系统媒体管理工具类
11
+ * @Prject: CommonUtilLibrary
12
+ * @Package: com.jingewenku.abrahamcaijin.commonutil
13
+ * @author: AbrahamCaiJin
14
+ * @date: 2017年06月01日 11:11
15
+ * @Copyright: 个人版权所有
16
+ * @Company:
17
+ * @version: 1.0.0
18
+ */
19
+
20
+ public class MediaUtil {
21
+
22
+ private static final String TAG = MediaUtil .class .getSimpleName ();
23
+
24
+ public static final int PHOTO_REQUEST_GALLERY = 1000 ;
25
+ public static final int PHOTO_REQUEST_CAMERA = 1001 ;
26
+ public static final int PHOTO_REQUEST_CUT = 1002 ;
27
+
28
+ /**
29
+ * 进入系统拍照
30
+ * @param activity
31
+ * @param outputUri 照片输出路径 Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/image.jpg"))
32
+ */
33
+ public static void startActivityForCamera (Activity activity , int requestCode , Uri outputUri ) {
34
+ Intent intent = new Intent (MediaStore .ACTION_IMAGE_CAPTURE );
35
+ intent .addCategory (Intent .CATEGORY_DEFAULT );
36
+ // 制定图片保存路径
37
+ intent .putExtra (MediaStore .EXTRA_OUTPUT , outputUri );
38
+ activity .startActivityForResult (intent , requestCode );
39
+ }
40
+
41
+ /**
42
+ * 进入系统拍照 (输出为Bitmap)<br>
43
+ *
44
+ * 获得输出
45
+ * 在 @<code>onActivityResult</code>中<br>
46
+ * 通过@<code>Bitmap bitmap = (Bitmap)intent.data.getExtras().get("data")</code>获取<br>
47
+ *
48
+ * Tips: 返回的Bitmap并非原图的Bitmap而是经过压缩的Bitmap
49
+ * @param activity
50
+ *
51
+ */
52
+ public static void startActivityForCamera (Activity activity , int requestCode ) {
53
+ Intent intent = new Intent (MediaStore .ACTION_IMAGE_CAPTURE );
54
+ intent .addCategory (Intent .CATEGORY_DEFAULT );
55
+ activity .startActivityForResult (intent , requestCode );
56
+ }
57
+
58
+ /**
59
+ * 进入系统图库<br>
60
+ * 获得输出<br>
61
+ * 在 @<code>onActivityResult</code>中通过@<code>Uri uri = intent.getData()</code>获取<br>
62
+ * Uri返回路径格式为 content://media/external/images/media/32073<br>
63
+ * 需要经过转换才能获得绝对路径
64
+ * @param activity
65
+ */
66
+ public static void startActivityForGallery (Activity activity , int requestCode ) {
67
+ // 弹出系统图库
68
+ Intent i = new Intent (Intent .ACTION_PICK ,
69
+ MediaStore .Images .Media .EXTERNAL_CONTENT_URI );
70
+ activity .startActivityForResult (i , requestCode );
71
+ }
72
+
73
+ /**
74
+ * 进入系统裁剪
75
+ * @param inputUri 需裁剪的图片路径 Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/image.jpg")
76
+ * @param outputUri 裁剪后图片路径 Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/image_cut.jpg")
77
+ * @param width 裁剪后宽度(px)
78
+ * @param height 裁剪后高度(px)
79
+ */
80
+ public static void startActivityForImageCut (Activity activity , int requestCode ,
81
+ Uri inputUri , Uri outputUri ,
82
+ int width , int height ) {
83
+ Intent intent = new Intent ("com.android.camera.action.CROP" );
84
+ intent .setDataAndType (inputUri , "image/*" );
85
+ // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
86
+ intent .putExtra ("crop" , "true" );
87
+ intent .putExtra ("scale" , true ); // 去黑边
88
+ intent .putExtra ("scaleUpIfNeeded" , true ); // 去黑边
89
+ // aspectX aspectY 裁剪框宽高比例
90
+ intent .putExtra ("aspectX" , width ); // 输出是X方向的比例
91
+ intent .putExtra ("aspectY" , height );
92
+ // outputX outputY 输出图片宽高,切忌不要再改动下列数字,会卡死
93
+ intent .putExtra ("outputX" , width ); // 输出X方向的像素
94
+ intent .putExtra ("outputY" , height );
95
+ intent .putExtra ("outputFormat" , Bitmap .CompressFormat .JPEG .toString ());
96
+ intent .putExtra ("noFaceDetection" , true );
97
+ intent .putExtra (MediaStore .EXTRA_OUTPUT , outputUri );
98
+ intent .putExtra ("return-data" , false ); // 设置为不返回数据
99
+ activity .startActivityForResult (intent , requestCode );
100
+ }
101
+ }
0 commit comments