下面是效果图,看看是不是亲想要的效果图,如果是,这段代码你就可以参考下了,但是要灵活运用,根据需求做相应的改动。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <button android:id= "@+id/take_photo" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "take photo" /> <button android:id= "@+id/get_photo" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "get photo" /> <imageview android:id= "@+id/picture" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_horizontal" /> </linearlayout> |
package com.example.choosepictest;
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
|
import java.io.file; import java.io.ioexception; import android.app.activity; import android.content.intent; import android.graphics.bitmap; import android.net.uri; import android.os.bundle; import android.os.environment; import android.provider.mediastore; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.imageview; public class mainactivity extends activity implements onclicklistener { public static final int take_photo = 1 ; public static final int crop_photo = 2 ; public static final int get_photo = 3 ; private button takephoto; private button getphoto; private imageview picture; private uri headimguri; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); takephoto = (button) findviewbyid(r.id.take_photo); getphoto = (button) findviewbyid(r.id.get_photo); picture = (imageview) findviewbyid(r.id.picture); takephoto.setonclicklistener( this ); getphoto.setonclicklistener( this ); } @override public void onclick(view v) { switch (v.getid()) { case r.id.take_photo: takephoto(); break ; case r.id.get_photo: getphoto(); break ; default : break ; } } // 拍照 private void takephoto() { file appdir = new file(environment.getexternalstoragedirectory(), "/etoury/piccache" ); if (!appdir.exists()) { appdir.mkdir(); } string filename = "user_head" + ".jpg" ; file outputimage = new file(appdir, filename); try { if (outputimage.exists()) { outputimage.delete(); } outputimage.createnewfile(); } catch (ioexception e) { e.printstacktrace(); } headimguri = uri.fromfile(outputimage); intent intent = new intent( "android.media.action.image_capture" ); intent.putextra(mediastore.extra_output, headimguri); startactivityforresult(intent, take_photo); } // 定向到图片库 private void getphoto() { intent intent = new intent(intent.action_pick, android.provider.mediastore.images.media.external_content_uri); startactivityforresult(intent, get_photo); } /** * 裁剪 */ private void crop(uri uri) { // 裁剪图片意图 intent intent = new intent( "com.android.camera.action.crop" ); intent.setdataandtype(uri, "image/*" ); // 下面这个crop=true是设置在开启的intent中设置显示的view可裁剪 intent.putextra( "crop" , "true" ); intent.putextra( "scale" , true ); // 去黑边 // 裁剪框的比例,1:1 intent.putextra( "aspectx" , 1 ); // 输出是x方向的比例 intent.putextra( "aspecty" , 1 ); // 裁剪后输出图片的尺寸大小,不能太大500程序崩溃 intent.putextra( "outputx" , 256 ); intent.putextra( "outputy" , 256 ); // 图片格式 /* intent.putextra( "outputformat" , "jpeg" ); */ intent.putextra( "outputformat" , bitmap.compressformat.jpeg.tostring()); // intent.putextra("nofacedetection", true);// 取消人脸识别 intent.putextra( "return-data" , true ); // true:返回uri,false:不返回uri // 同一个地址下 裁剪的图片覆盖拍照的图片 intent.putextra(mediastore.extra_output, headimguri); startactivityforresult(intent, crop_photo); } @override protected void onactivityresult( int requestcode, int resultcode, intent data) { switch (requestcode) { case get_photo: if (resultcode == result_ok) { crop(data.getdata()); } break ; case take_photo: if (resultcode == result_ok) { crop(headimguri); } break ; case crop_photo: if (resultcode == result_ok) { bitmap cropbitmap = data.getparcelableextra( "data" ); picture.setimagebitmap(cropbitmap); } break ; default : break ; } } } |
总结:
1. 拍照返回一张图片,可以是全尺寸的图片
2. 拍照返回图片的地址问题,一个目录下的一个文件
3. 裁剪的图片的地址, 覆盖了全尺寸图片的地址
4. 相册intent 返回的是一个uir , 不是string
5. 裁剪的图片,不能覆盖相册返回的uri(一定注意)