android 拍照上传照片
来源:互联网 发布:万游网络传奇 编辑:程序博客网 时间:2024/05/01 13:45
http://www.open-open.com/lib/view/open1329141214687.html
废话不多说,直接进入主题,想要在android中实现拍照最简单饿方法就是New 一个 Intent 设置Action为android.media.action.IMAGE_CAPTURE 然后使用startActivityForResult(intent,REQUEST_CODE)方法进入相机。当然还有很多方式可以实现,大家可以在网上查找。但是要注意的是在进入相机前最好判断下sdcard是否可用,代码如下:
1
destoryBimap();
2
String state = Environment.getExternalStorageState();
3
if
(state.equals(Environment.MEDIA_MOUNTED)) {
4
intent =
new
Intent(
"android.media.action.IMAGE_CAPTURE"
);
5
startActivityForResult(intent, REQUEST_CODE);
6
}
else
{
7
Toast.makeText(DefectManagerActivity.
this
,
8
R.string.common_msg_nosdcard, Toast.LENGTH_LONG).show();
9
}
01
Uri uri = data.getData();
02
if
(uri !=
null
) {
03
photo = BitmapFactory.decodeFile(uri.getPath());
04
}
05
if
(photo ==
null
) {
06
Bundle bundle = data.getExtras();
07
if
(bundle !=
null
) {
08
photo = (Bitmap) bundle.get(
"data"
);
09
}
else
{
10
Toast.makeText(DefectManagerActivity.
this
,
11
getString(R.string.common_msg_get_photo_failure),
12
Toast.LENGTH_LONG).show();
13
return
;
14
}
15
}
第一种方式是用方法中传回来的intent调用getData();方法获取数据的Uri,然后再根据uri获取数据的路径,然后根据路径封装成一个bitmap就行了.
第二种方式也是用法中传回来的intent对象但是不再是调用getData();方法而是调用getExtras();方法获取intent里面所有参数的一个对象集合bundle,然后是用bundle对象得到键为data的值也就是一个bitmap对象.
通过上面两种方式就能获取相片的bitmap对象,然后就可以在程序中是用,如果你想把相片保存到自己指定的目录可以是用如下步骤即可:
首先bitmap有个一compress(Bitmap.CompressFormat.JPEG, 100, baos)方法,这个方法有三个参数,第一个是指定将要保存的图片的格式,第二个是图片保存的质量,值是0-100,比如像PNG格式的图片这个参数你可以随便设置,因为PNG是无损的格式。第三个参数是你一个缓冲输出流ByteArrayOutputStream();,这个方法的作用就是把 bitmap的图片转换成jpge的格式放入输出流中,然后大家应该明白怎么操作了吧,下面是实例代码:
01
String pictureDir =
""
;
02
FileOutputStream fos =
null
;
03
BufferedOutputStream bos =
null
;
04
ByteArrayOutputStream baos =
null
;
05
try
{
06
baos =
new
ByteArrayOutputStream();
07
bitmap.compress(Bitmap.CompressFormat.JPEG,
100
, baos);
08
byte
[] byteArray = baos.toByteArray();
09
String saveDir = Environment.getExternalStorageDirectory()
10
+
"/temple"
;
11
File dir =
new
File(saveDir);
12
if
(!dir.exists()) {
13
dir.mkdir();
14
}
15
File file =
new
File(saveDir,
"temp.jpg"
);
16
file.delete();
17
if
(!file.exists()) {
18
file.createNewFile();
19
}
20
fos =
new
FileOutputStream(file);
21
bos =
new
BufferedOutputStream(fos);
22
bos.write(byteArray);
23
pictureDir = file.getPath();
24
}
catch
(Exception e) {
25
e.printStackTrace();
26
}
finally
{
27
if
(baos !=
null
) {
28
try
{
29
baos.close();
30
}
catch
(Exception e) {
31
e.printStackTrace();
32
}
33
}
34
if
(bos !=
null
) {
35
try
{
36
bos.close();
37
}
catch
(Exception e) {
38
e.printStackTrace();
39
}
40
}
41
if
(fos !=
null
) {
42
try
{
43
fos.close();
44
}
catch
(Exception e) {
45
e.printStackTrace();
46
}
47
}
48
}
01
/**
02
* 提交参数里有文件的数据
03
*
04
* @param url
05
* 服务器地址
06
* @param param
07
* 参数
08
* @return 服务器返回结果
09
* @throws Exception
10
*/
11
public
static
String uploadSubmit(String url, Map<String, String> param,
12
File file)
throws
Exception {
13
HttpPost post =
new
HttpPost(url);
14
15
MultipartEntity entity =
new
MultipartEntity();
16
if
(param !=
null
&& !param.isEmpty()) {
17
for
(Map.Entry<String, String> entry : param.entrySet()) {
18
entity.addPart(entry.getKey(),
new
StringBody(entry.getValue()));
19
}
20
}
21
// 添加文件参数
22
if
(file !=
null
&& file.exists()) {
23
entity.addPart(
"file"
,
new
FileBody(file));
24
}
25
post.setEntity(entity);
26
HttpResponse response = httpClient.execute(post);
27
int
stateCode = response.getStatusLine().getStatusCode();
28
StringBuffer sb =
new
StringBuffer();
29
if
(stateCode == HttpStatus.SC_OK) {
30
HttpEntity result = response.getEntity();
31
if
(result !=
null
) {
32
InputStream is = result.getContent();
33
BufferedReader br =
new
BufferedReader(
34
new
InputStreamReader(is));
35
String tempLine;
36
while
((tempLine = br.readLine()) !=
null
) {
37
sb.append(tempLine);
38
}
39
}
40
}
41
post.abort();
42
return
sb.toString();
43
}
1
/**
2
* 销毁图片文件
3
*/
4
private
void
destoryBimap() {
5
if
(photo !=
null
&& !photo.isRecycled()) {
6
photo.recycle();
7
photo =
null
;
8
}
9
}
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片(新)
- android 拍照上传照片(新)
- android 拍照上传照片(新)
- android 拍照上传照片(新)
- Android拍照或相册获取照片上传
- 拍照-照片-上传
- android 实现调用相机拍照 获取照片路径并上传
- android 选择图片拍照并剪切照片上传到服务器
- Android拍照或图库选择照片上传(附服务器端代码)
- SQL查询表名
- asp操作有返回值的存储过程
- jms createSession详解
- android上传图片以及POST字符串
- Re-installation failed due to different application signatures.
- android 拍照上传照片
- 几种函数调用方式
- linux网络相关设置
- android上传图片至服务器
- 软件测试面试题及解析(十一)
- 拓扑排序算法
- Codeforces 217A. Ice Skating 搜索
- 开始学习SQLite
- NSInvocaion用来替代performSelector传多个参数时,以及根据函数签名来动态调用时