简单的 Android 拍照并显示以及获取路径后上传

Activity 中的代码,我只贴出重要的事件部分代码

    public void doPhoto(View view)
{
destoryBimap();
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 1);
} else {
Toast.makeText(MainActivity.this, "没有SD卡", Toast.LENGTH_LONG).show();
}
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Uri uri = data.getData();
if (uri != null) {
this.photo = BitmapFactory.decodeFile(uri.getPath());
}
if (this.photo == null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
this.photo = (Bitmap) bundle.get("data");
} else {
Toast.makeText(MainActivity.this, "拍照失败", Toast.LENGTH_LONG).show();
return;
}
} FileOutputStream fileOutputStream = null;
try {
// 获取 SD 卡根目录
String saveDir = Environment.getExternalStorageDirectory() + "/meitian_photos";
// 新建目录
File dir = new File(saveDir);
if (! dir.exists()) dir.mkdir();
// 生成文件名
SimpleDateFormat t = new SimpleDateFormat("yyyyMMddssSSS");
String filename = "MT" + (t.format(new Date())) + ".jpg";
// 新建文件
File file = new File(saveDir, filename);
// 打开文件输出流
fileOutputStream = new FileOutputStream(file);
// 生成图片文件
this.photo.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
// 相片的完整路径
this.picPath = file.getPath();
ImageView imageView = (ImageView) findViewById(R.id.showPhoto);
imageView.setImageBitmap(this.photo);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} /**
* 销毁图片文件
*/
private void destoryBimap()
{
if (photo != null && ! photo.isRecycled()) {
photo.recycle();
photo = null;
}
}

Layout 布局页面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/doPhoto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginBottom="10dp"
android:text="拍照"
android:onClick="doPhoto"
/>
<TextView
android:id="@+id/showContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
/>
<ImageView
android:id="@+id/showPhoto"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:src="@drawable/add"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>

其中的上传工具类请查看该文章:

http://blog.csdn.net/zhouzme/article/details/18952053

简单的 Android 拍照并显示以及获取路径后上传的更多相关文章

  1. 关于web项目中的图片上传、并在前端显示问题(tomcat中配置文件上传虚拟路径)

    一.数据库存储 直接把图片的二进制码存到数据库,可参考blog:http://blog.csdn.net/hope2jiang/article/details/590733 直接存图片在mysql上面 ...

  2. 【转】关于web项目中的图片上传、并在前端显示问题(tomcat中配置文件上传虚拟路径)

    一.数据库存储 直接把图片的二进制码存到数据库,可参考blog:http://blog.csdn.net/hope2jiang/article/details/590733 直接存图片在mysql上面 ...

  3. Springboot框架中request.getInputStream()获取不到上传的文件流

    Springboot框架中用下面的代码,使用request.getInputStream()获取不到上传的文件流 @PostMapping("/upload_img") publi ...

  4. Android 拍照或者从相册获取图片的实现

    我们常常会用到上传头像,或者发帖子的时候选择本地图片上传的功能.这个很常见 今天因为app的需求我研究了下.现在分享下. 其实不论是通过拍照还是从相册选取都会用到Intent 这是系统提供给我们用来调 ...

  5. Android拍照和从相册获取照片

    1.从相册获取照片 private void openAlumb() { //mRxPermissions:三方权限库 mRxPermissions .request(Manifest.permiss ...

  6. Android获取通讯录并上传(包含通讯录加密)

    好久没更新文章了,近期在做通讯录上传,把它分享出来,送给需要的朋友. 写了一个通讯录工具类,直接放代码吧,关键位置通过注释来解释. 这个工具类包含通讯录获取,加密,然后上传操作.看不懂的可以留言 im ...

  7. Android上传图片到PHP服务器并且支持浏览器上传文件(word、图片、音乐等)

    暑假已经过了一半了,这才完成计划当中的第二个任务.虽然进度是慢了点.但也算是暑假的收获吧.下面我就把我学习当中的收获记录在此. 还是跟以往一样,先上图片. 操作的步骤:打开程序---->选择上传 ...

  8. (转)Android学习-使用Async-Http实现图片压缩并上传功能

    (转)Android学习-使用Async-Http实现图片压缩并上传功能 文章转载自:作者:RyaneLee链接:http://www.jianshu.com/p/940fc7ba39e1 让我头疼一 ...

  9. Android学习笔记_13_网络通信之多个上传文件

    一.获取HTTP协议: 建立一个Web项目,建立一个如下所示的jsp界面,用IE捕获表单提交信息. <%@ page language="java" contentType= ...

随机推荐

  1. 无线网卡连接internet,有线网卡向另一台电脑分享网络(笔记本当有线路由器)

    有一台笔记本和一台台式机,都放在卧室内 笔记本能用无线网卡连接到客厅的路由器,台式机没有配备无线网卡,不能上网 要从客厅的路由器那边拉一条网线到卧室内连接台式机显然很蠢,在卧室内购置一台中继路由器也不 ...

  2. Java Web学习笔记--JSP for循环

    JSP for循环 <%@ page language="java" contentType="text/html; charset=UTF-8" %&g ...

  3. Python 3

    #对于任意可迭代对象可使用序列拆分操作符*进行拆分 #可用iterable对分片进行赋值等操作(相当于del分片并把iterable插入其中,所以二者长度可以不同) #del取消对象引用与数据项之间的 ...

  4. 安卓自动化测试工具一:Monkey

    一:monkey的用途:主要用于稳定性测试,模拟用户操作 二.monkey的基本使用 monkey文档地址:"<android_sdk>/docs/tools/help/monk ...

  5. 一台服务器上同时启动多个 Tomcat

    在同一台服务器上启动多个 Tomcat 服务,需要修改 conf/server.xml文件里的三个部分,如下: 1.修改Http访问端口(默认为8080端口) <Connector port=& ...

  6. Oracle 视图操作

    -- 创建视图create view V_TEST asselect * from T_TEST where rownum <100 select * from V_TEST-- 新建视图用户 ...

  7. 关于C#继承运用的总结

    整体代码部分: 解决方案: 父类Person类: using System; using System.Collections.Generic; using System.Linq; using Sy ...

  8. 关于Kafka使用IBM Java报错解决方案

    安装环境 Ubuntu 14.04 Java IBM Java 1.7.0_79 Kakfa 2.10-0.8.2.1 使用bin/kafka-server-start.sh config/serve ...

  9. linux 下查看cpu是几核的

    几个cpu more /proc/cpuinfo |grep "physical id"|uniq|wc -l 每个cpu是几核(假设cpu配置相同) more /proc/cpu ...

  10. WPF中自定义绘制内容

    先说结论:实现了在自定义大小的窗口中,加载图片,并在图片上绘制一个矩形框:且在窗口大小改变的情况,保持绘制的矩形框与图片的先对位置不变. 在WinForm中,我们可以很方便地绘制自己需要的内容,在WP ...