android 拍照上传照片(新)
来源:互联网 发布:c语言ppt 谭浩强 编辑:程序博客网 时间:2024/05/01 18:09
首先当我们要得到原有的照片必须为拍照后的照片指定存放的路径地址,这个地址是在Intent中指定,方法是intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
其中file就是手机本地的对应的输出文件,这个需要在传入参数前就生成,不然会报FileNotFoundException,下面是源码:
[java]
destoryBimap();
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
String saveDir = Environment.getExternalStorageDirectory()
+ "/temple";
File dir = new File(saveDir);
if (!dir.exists()) {
dir.mkdir();
}
file = new File(saveDir, "temp.jpg");
file.delete();
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(ReportSurveyPointActivity.this,
R.string.photo_file_isnull,
Toast.LENGTH_LONG).show();
return;
}
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, REQUEST_CODE);
} else {
Toast.makeText(ReportSurveyPointActivity.this,
R.string.common_msg_nosdcard, Toast.LENGTH_LONG)
.show();
}
其中destoryBimap()这个方法已经在上一篇文章中介绍过了,是为了把照片从手机内存中清楚调,不然黑容易报内存溢出异常,毕竟原图片比较大。下面就是判断手机是否装载SDCard,接下来在SDCard上生成我们照片保存的地址。
那么接下来就是在onActivityResult()方法中加载我们的照片,这下加载照片就非常简单了,因为在拍照前我们已经给除了照片的路径,我们根据这个路径把照片加载进来就行了,下面是源码:
[java]
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode != RESULT_OK) {
return;
}
if (file != null && file.exists()) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
photo = BitmapFactory.decodeFile(file.getPath(), options);
photo_image.setBackgroundDrawable(new BitmapDrawable(photo));
pictureDir = file.getPath();
} else {
Toast.makeText(this, R.string.photo_file_isnull,
Toast.LENGTH_LONG).show();
}
}
}
这里需要说明一下的就是,当我们要在程序预览照片的时候由于照片比较大,考虑到内存问题,所以我们要把照片进行压缩再放到控件里面,压缩照片的方法就是用BitmapFactory.Options类的inSampleSize这个属性,这个属性的意思说在原照片的大小的多少比例来展示相片,也就是个百分比。我这里是设置的2,也就是说原照片的一般的大小展示。
下面是销毁照片的方法:
[java]
/**
* 销毁图片文件
*/
private void destoryBimap() {
if (photo != null && !photo.isRecycled()) {
photo.recycle();
photo = null;
}
}
这个方法我是在每次拍照前和onDestroy()方法中都调用了的,都是处于内存考虑,如果你是dialog的话还建议在dimisslistener中调。
至于大家经常问道的上传照片到后台的方法我也顺便说下,我是用的HttpClient实现的很简单,而后台就是随便什么框架都行,下面是上传照片的方法实现:
[java]
/**
* 提交参数里有文件的数据
*
* @param url
* 服务器地址
* @param param
* 参数
* @return 服务器返回结果
* @throws Exception
*/
public static String uploadSubmit(String url, Map<String, String> param,
File file) throws Exception {
HttpPost post = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
if (param != null && !param.isEmpty()) {
for (Map.Entry<String, String> entry : param.entrySet()) {
if (entry.getValue() != null
&& entry.getValue().trim().length() > 0) {
entity.addPart(entry.getKey(),
new StringBody(entry.getValue()));
}
}
}
// 添加文件参数
if (file != null && file.exists()) {
entity.addPart("file", new FileBody(file));
}
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
int stateCode = response.getStatusLine().getStatusCode();
StringBuffer sb = new StringBuffer();
if (stateCode == HttpStatus.SC_OK) {
HttpEntity result = response.getEntity();
if (result != null) {
InputStream is = result.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String tempLine;
while ((tempLine = br.readLine()) != null) {
sb.append(tempLine);
}
} www.2cto.com
}
post.abort();
return sb.toString();
}
- android 拍照上传照片(新)
- android 拍照上传照片(新)
- android 拍照上传照片(新)
- android 拍照上传照片(新)
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- Android拍照或相册获取照片上传
- 拍照-照片-上传
- android 实现调用相机拍照 获取照片路径并上传
- android 选择图片拍照并剪切照片上传到服务器
- Android拍照或图库选择照片上传(附服务器端代码)
- Android:文件下载和写入SD卡学习小结
- 程序员常去的14个顶级开发社区
- Tomcat负载均衡原理详解及配置(Apache2.2.19+Tomcat7.0.12) mod_proxy_blancer方式已经成功实现
- 6410 wicne6.0R3 IE浏览器显示问题
- ts与安卓语言文件string.xml的转换工具
- android 拍照上传照片(新)
- 2012年部分项目经验
- 软件质量造成的影响
- c++基础学习7-c++构造函数和析构函数
- VMware安装系统报错
- 【转】SQL Server日期时间格式转换字符串
- 2013年部分项目经验
- BlueTooth蓝牙核心技术(一)
- C++ 中的 new/delete 和 new[]/delete[]