1.实现图片的选择触发点的布局
布局代码:
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
|
<
RelativeLayout
android
:
layout_width
=
"match_parent"
android
:
layout_height
=
"96dp"
android
:
layout_marginTop
=
"18dip"
android
:
background
=
"@drawable/mm_listitem"
android
:
gravity
=
"center_vertical"
>
<
!
--
<
ImageView
android
:
id
=
"@+id/edit_background_person_icon"
android
:
layout_width
=
"96dp"
android
:
layout_height
=
"96dp"
android
:
layout_alignParentRight
=
"true"
android
:
layout_centerVertical
=
"true"
android
:
layout_marginRight
=
"10dp"
android
:
background
=
"@drawable/noicon"
/
>
--
>
<
ImageView
android
:
id
=
"@+id/add_noicon"
android
:
layout_marginRight
=
"28dp"
android
:
layout_width
=
"60dp"
android
:
layout_height
=
"60dp"
android
:
scaleType
=
"fitXY"
android
:
layout_alignParentRight
=
"true"
android
:
layout_centerVertical
=
"true"
android
:
src
=
"@drawable/yunshu"
/
>
<
TextView
android
:
layout_width
=
"wrap_content"
android
:
layout_height
=
"wrap_content"
android
:
layout_centerVertical
=
"true"
android
:
padding
=
"7dp"
android
:
text
=
"头像"
android
:
textSize
=
"20sp"
android
:
textColor
=
"#000"
/
>
<
/
RelativeLayout
>
|
实现选择之后的弹出窗口,这里使用popwindow来实现。
布局实现:
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
|
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns
:
android
=
"http://schemas.android.com/apk/res/android"
android
:
layout_width
=
"296dp"
android
:
layout_height
=
"wrap_content"
android
:
background
=
"@drawable/popmenu_bg"
android
:
orientation
=
"vertical"
android
:
layout_marginRight
=
"48dp"
android
:
paddingBottom
=
"8.0dip"
android
:
paddingLeft
=
"8.0dip"
android
:
layout_gravity
=
"center"
android
:
gravity
=
"center"
android
:
paddingRight
=
"8.0dip"
android
:
paddingTop
=
"16.0dip"
>
<
Button
android
:
layout_width
=
"144dp"
android
:
layout_height
=
"wrap_content"
android
:
text
=
"选择图片"
android
:
id
=
"@+id/selectImage"
/
>
<
Button
android
:
layout_width
=
"144dp"
android
:
layout_height
=
"wrap_content"
android
:
text
=
"拍照"
android
:
id
=
"@+id/takeImage"
/
>
<
/
LinearLayout
>
|
弹出窗口的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
|
package
com
.
coolbeetle
.
assistant
.
menu
;
import
android
.
content
.
Context
;
import
android
.
graphics
.
Color
;
import
android
.
graphics
.
drawable
.
ColorDrawable
;
import
android
.
view
.
LayoutInflater
;
import
android
.
view
.
View
;
import
android
.
view
.
View
.
OnClickListener
;
import
android
.
view
.
ViewGroup
.
LayoutParams
;
import
android
.
widget
.
PopupWindow
;
import
com
.
coolbeetle
.
assistant
.
R
;
import
com
.
coolbeetle
.
assistant
.
menu
.
PopMenu
.
OnPopExternalItemClickListener
;
/**
* upload popup implementation
*
* @author coolbeetle
* @since 1.0
*/
public
class
PopUpload
{
private
Context
context
;
private
PopupWindow
popupWindow
;
private
OnClickListener
listener
;
private
LayoutInflater
inflater
;
public
PopUpload
(
Context
context
,
OnClickListener
listener
)
{
this
.
listener
=
listener
;
this
.
context
=
context
;
this
.
inflater
=
(
LayoutInflater
)
context
.
getSystemService
(
Context
.
LAYOUT_INFLATER_SERVICE
)
;
View
view
=
inflater
.
inflate
(
R
.
layout
.
image_upload
,
null
)
;
view
.
findViewById
(
R
.
id
.
selectImage
)
.
setOnClickListener
(
this
.
listener
)
;
view
.
findViewById
(
R
.
id
.
takeImage
)
.
setOnClickListener
(
this
.
listener
)
;
popupWindow
=
new
PopupWindow
(
view
,
context
.
getResources
(
)
.
getDimensionPixelSize
(
R
.
dimen
.
popupload_width
)
,
// 这里宽度需要自己指定,使用
// WRAP_CONTENT
// 会很大
LayoutParams
.
WRAP_CONTENT
)
;
popupWindow
.
setBackgroundDrawable
(
new
ColorDrawable
(
Color
.
TRANSPARENT
)
)
;
}
public
void
setListener
(
OnClickListener
listener
)
{
this
.
listener
=
listener
;
}
// 下拉式 弹出 pop菜单 parent 右下角
public
void
showAsDropDown
(
View
parent
)
{
popupWindow
.
showAsDropDown
(
parent
,
10
,
// 保证尺寸是根据屏幕像素密度来的
context
.
getResources
(
)
.
getDimensionPixelSize
(
R
.
dimen
.
popmenu_yoff
)
)
;
// 使其聚集
popupWindow
.
setFocusable
(
true
)
;
// 设置允许在外点击消失
popupWindow
.
setOutsideTouchable
(
true
)
;
// 刷新状态
popupWindow
.
update
(
)
;
}
// 隐藏菜单
public
void
dismiss
(
)
{
popupWindow
.
dismiss
(
)
;
}
}
|
对于拍照的选择,这里使用了一个专门的Activity来实现:
布局实现:
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
|
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
RelativeLayout
xmlns
:
android
=
"http://schemas.android.com/apk/res/android"
android
:
layout_width
=
"fill_parent"
android
:
layout_height
=
"wrap_content"
android
:
gravity
=
"center_horizontal"
android
:
orientation
=
"vertical"
>
<
LinearLayout
android
:
id
=
"@+id/dialog_layout"
android
:
layout_width
=
"fill_parent"
android
:
layout_height
=
"wrap_content"
android
:
layout_alignParentBottom
=
"true"
android
:
background
=
"@drawable/btn_style_alert_dialog_background"
android
:
gravity
=
"center_horizontal"
android
:
orientation
=
"vertical"
>
<
Button
android
:
id
=
"@+id/btn_take_photo"
android
:
layout_width
=
"fill_parent"
android
:
layout_height
=
"wrap_content"
android
:
layout_marginLeft
=
"20dip"
android
:
layout_marginRight
=
"20dip"
android
:
layout_marginTop
=
"20dip"
android
:
background
=
"@drawable/btn_style_alert_dialog_button"
android
:
text
=
"拍照"
android
:
textStyle
=
"bold"
/
>
<
Button
android
:
id
=
"@+id/btn_cancel"
android
:
layout_width
=
"fill_parent"
android
:
layout_height
=
"wrap_content"
android
:
layout_marginBottom
=
"15dip"
android
:
layout_marginLeft
=
"20dip"
android
:
layout_marginRight
=
"20dip"
android
:
layout_marginTop
=
"15dip"
android
:
background
=
"@drawable/btn_style_alert_dialog_cancel"
android
:
text
=
"取消"
android
:
textColor
=
"#ffffff"
android
:
textStyle
=
"bold"
/
>
<
/
LinearLayout
>
<
/
RelativeLayout
>
|
Activity的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
|
package
com
.
coolbeetle
.
assistant
;
import
android
.
app
.
Activity
;
import
android
.
content
.
ContentValues
;
import
android
.
content
.
Intent
;
import
android
.
database
.
Cursor
;
import
android
.
net
.
Uri
;
import
android
.
os
.
Bundle
;
import
android
.
os
.
Environment
;
import
android
.
provider
.
MediaStore
;
import
android
.
util
.
Log
;
import
android
.
view
.
View
;
import
android
.
view
.
View
.
OnClickListener
;
import
android
.
widget
.
Button
;
import
android
.
widget
.
LinearLayout
;
import
android
.
widget
.
Toast
;
//选择拍照文件操作类
public
class
TakePhotoActivity
extends
Activity
implements
OnClickListener
{
private
LinearLayout
dialogLayout
;
//取消拍照
private
Button
takePhotoBtn
;
//点击拍照
private
Button
cancelBtn
;
private
Uri
photoUri
;
//自动获取照片uri
private
String
picPath
;
//图片路径
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
)
;
setContentView
(
R
.
layout
.
select_pic_layout
)
;
dialogLayout
=
(
LinearLayout
)
findViewById
(
R
.
id
.
dialog_layout
)
;
dialogLayout
.
setOnClickListener
(
this
)
;
cancelBtn
=
(
Button
)
findViewById
(
R
.
id
.
btn_cancel
)
;
cancelBtn
.
setOnClickListener
(
this
)
;
takePhotoBtn
=
(
Button
)
findViewById
(
R
.
id
.
btn_take_photo
)
;
takePhotoBtn
.
setOnClickListener
(
this
)
;
}
public
void
onClick
(
View
v
)
{
switch
(
v
.
getId
(
)
)
{
case
R
.
id
.
dialog_layout
:
finish
(
)
;
break
;
case
R
.
id
.
btn_take_photo
:
takePhoto
(
)
;
break
;
//采到了case中没列出来的状态,如cancelBtn
default
:
finish
(
)
;
break
;
}
}
// 点击拍照
private
void
takePhoto
(
)
{
//执行拍照前,应该先判断SD卡是否存在
try
{
String
SDState
=
Environment
.
getExternalStorageState
(
)
;
if
(
SDState
.
equals
(
Environment
.
MEDIA_MOUNTED
)
)
//如果有媒体安装的环境
{
Intent
intent
=
new
Intent
(
MediaStore
.
ACTION_IMAGE_CAPTURE
)
;
//传拍的照
/***
* 需要说明一下,以下操作使用+拍照,拍照后的图片会存放在相册中的
* 这里使用的这种方式有一个好处就是获取的图片是拍照后的原图
* 如果不使用ContentValues存放照片路径的话,拍照后获取的图片为缩略图不清晰
*/
ContentValues
values
=
new
ContentValues
(
)
;
//contentProvider存储?
photoUri
=
this
.
getContentResolver
(
)
.
insert
(
MediaStore
.
Images
.
Media
.
EXTERNAL_CONTENT_URI
,
values
)
;
//自动获得uri
intent
.
putExtra
(
android
.
provider
.
MediaStore
.
EXTRA_OUTPUT
,
photoUri
)
;
/**-----------------*/
startActivityForResult
(
intent
,
1
)
;
//跳到拍照页面,这里1没用到,可以在一个onActivityResult里设置requestCode为0来接收新页面的数据。
}
else
{
Toast
.
makeText
(
this
,
"内存卡不存在"
,
Toast
.
LENGTH_LONG
)
.
show
(
)
;
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
}
}
@Override
protected
void
onActivityResult
(
int
requestCode
,
int
resultCode
,
Intent
data
)
{
//这里的三个参数都没用,没有跳再新的页面传值回来
if
(
resultCode
==
Activity
.
RESULT_OK
&&
requestCode
==
1
)
//&&的后面可有可无,因为只有一个返回结果,若多个就可用switch case判断一下。
{
String
[
]
pojo
=
{
MediaStore
.
Images
.
Media
.
DATA
}
;
Cursor
cursor
=
managedQuery
(
photoUri
,
pojo
,
null
,
null
,
null
)
;
//Uri 和 图片数据 得到 picPath
if
(
cursor
!=
null
)
{
int
columnIndex
=
cursor
.
getColumnIndexOrThrow
(
pojo
[
0
]
)
;
cursor
.
moveToFirst
(
)
;
picPath
=
cursor
.
getString
(
columnIndex
)
;
cursor
.
close
(
)
;
}
Log
.
i
(
"SelectPicActivity---------------"
,
"imagePath = "
+
picPath
)
;
/*if(picPath != null && ( picPath.endsWith(".png") || picPath.endsWith(".PNG") ||picPath.endsWith(".jpg") ||picPath.endsWith(".JPG") ))*/
if
(
picPath
!=
null
)
{
Intent
lastIntent
=
getIntent
(
)
;
//上面putExtra 这里getIntent
lastIntent
.
putExtra
(
"photo_path"
,
picPath
)
;
//这是新页面,,再传送路径 返送回去EmployeeAttendanceActivity
setResult
(
Activity
.
RESULT_OK
,
lastIntent
)
;
finish
(
)
;
}
else
{
Toast
.
makeText
(
this
,
"选择文件不正确!"
,
Toast
.
LENGTH_LONG
)
.
show
(
)
;
}
}
super
.
onActivityResult
(
requestCode
,
resultCode
,
data
)
;
}
}
|
最后这里看一下 主Activity中是如何触发从相册选择图片和拍照选择图片的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@
Override
public
void
onClick
(
View
v
)
{
switch
(
v
.
getId
(
)
)
{
case
R
.
id
.
add_noicon
:
this
.
uploadPop
.
showAsDropDown
(
v
)
;
break
;
case
R
.
id
.
selectImage
:
Intent
intent
=
new
Intent
(
)
;
// intent可以过滤图片文件或其他的
intent
.
setType
(
"image/*"
)
;
intent
.
setAction
(
Intent
.
ACTION_GET_CONTENT
)
;
startActivityForResult
(
intent
,
22
)
;
break
;
case
R
.
id
.
takeImage
:
Intent
intent2
=
new
Intent
(
getApplicationContext
(
)
,
TakePhotoActivity
.
class
)
;
startActivityForResult
(
intent2
,
23
)
;
break
;
}
}
|
这里使用Activity的startActivytForResult这个方法,根据返回的status 码不同来分辨是相册选择还是拍照选择。
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
|
@Override
protected
void
onActivityResult
(
int
requestCode
,
int
resultCode
,
Intent
data
)
{
if
(
resultCode
==
Activity
.
RESULT_OK
)
{
switch
(
requestCode
)
{
case
22
:
Uri
uri
=
data
.
getData
(
)
;
Log
.
e
(
"Upload主页---L92--获得相册图片---------"
,
"uri = "
+
uri
)
;
try
{
String
[
]
pojo
=
{
MediaStore
.
Images
.
Media
.
DATA
}
;
Cursor
cursor
=
managedQuery
(
uri
,
pojo
,
null
,
null
,
null
)
;
if
(
cursor
!=
null
)
{
int
colunm_index
=
cursor
.
getColumnIndexOrThrow
(
MediaStore
.
Images
.
Media
.
DATA
)
;
cursor
.
moveToFirst
(
)
;
String
path
=
cursor
.
getString
(
colunm_index
)
;
if
(
path
.
endsWith
(
"jpg"
)
||
path
.
endsWith
(
"png"
)
)
{
picPath
=
path
;
ContentResolver
cr
=
this
.
getContentResolver
(
)
;
BitmapFactory
.
Options
options
=
new
BitmapFactory
.
Options
(
)
;
options
.
inSampleSize
=
2
;
Bitmap
bitmap
=
BitmapFactory
.
decodeStream
(
cr
.
openInputStream
(
uri
)
,
null
,
options
)
;
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
(
)
;
bitmap
.
compress
(
Bitmap
.
CompressFormat
.
JPEG
,
100
,
baos
)
;
imageStream
=
new
ByteArrayInputStream
(
baos
.
toByteArray
(
)
)
;
imageView
.
setImageBitmap
(
bitmap
)
;
}
else
{
alert
(
)
;
}
}
else
{
alert
(
)
;
}
}
catch
(
Exception
e
)
{
}
break
;
case
23
:
imageView
.
setImageBitmap
(
null
)
;
// 设置图片位图不设null的话,
// 第一次上传照片成功后,第二次上传照片会报错。
picPath
=
data
.
getStringExtra
(
"photo_path"
)
;
// 从SelectPicActivity.java中传回图片路径
Log
.
i
(
"uploadImage"
,
"最终选择的图片路径="
+
picPath
)
;
BitmapFactory
.
Options
options
=
new
BitmapFactory
.
Options
(
)
;
options
.
inSampleSize
=
2
;
Bitmap
bm
=
BitmapFactory
.
decodeFile
(
picPath
,
options
)
;
// 把获得的图片路径译码成位图
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
(
)
;
bm
.
compress
(
Bitmap
.
CompressFormat
.
JPEG
,
100
,
baos
)
;
imageStream
=
new
ByteArrayInputStream
(
baos
.
toByteArray
(
)
)
;
imageView
.
setImageBitmap
(
bm
)
;
// 把位图显示出来
break
;
}
// switch结束
requestUploadImage
(
)
;
}
// if结束
}
// onActivityResult结束
private
void
alert
(
)
{
Dialog
dialog
=
new
AlertDialog
.
Builder
(
this
)
.
setTitle
(
"提示"
)
.
setMessage
(
|