拍照并显示以及获取路径后上传
2015-09-09 14:28
387 查看
简单的 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;
}
}
<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" a
ndroid: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">
</imageview>
</textview>
</button>
</linearlayout>
</scrollview><
/linearlayout>
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;
}
}
<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" a
ndroid: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">
</imageview>
</textview>
</button>
</linearlayout>
</scrollview><
/linearlayout>
相关文章推荐
- Mysql 5.5 修改max_allowed_packet参数
- QQ互联不能使用的通用解决方法
- 阿里巴巴已拿下中国互联网半壁江山
- 编写一个服务端可以给多个客户端发送图片
- asp.net文件上传带进度条实现案例(多种风格)
- 开发者经验谈:如何一天时间搞定iOS游戏开发?
- 开发者经验谈:如何一天时间搞定iOS游戏开发?
- 开发者经验谈:如何一天时间搞定iOS游戏开发?
- 开发者经验谈:如何一天时间搞定iOS游戏开发?
- 开发者经验谈:如何一天时间搞定iOS游戏开发?
- 关于企业投资所得是否需要再交所得税的问题
- 注释笔记
- java图形界面gui
- 开发者经验谈:如何一天时间搞定iOS游戏开发?
- 今年Chinajoy除了showgirl看什么
- oracle设备
- django.core.exceptions.ImproperlyConfigured:
- [WEB前端开发]-VM中的POST请求-解决页面向control层传参的问题
- C++——运算符重载(三)
- abap转换时间为时间戳的函数IB_CONVERT_INTO_TIMESTAMP