Java乔晓松-android中调用系统拍照功能并显示拍照的图片
android中调用系统拍照功能并显示拍照的图片
如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图
代码如下:
package com.example.myphotos; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
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.ImageView; /**
* 2013-6-27 上午10:27:23
*
* @author 乔晓松
*/
public class CameraActivity extends Activity { private Button button;
private ImageView imageView;
private String fileName; @SuppressLint({ "SimpleDateFormat", "SdCardPath" })
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_camera);
button = (Button) findViewById(R.id.btn_camera);
imageView = (ImageView) findViewById(R.id.imageView1);
File file = new File("/sdcard/myImage/");
file.mkdirs();// 创建文件夹 button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
}
});
// Intent intent = new Intent();
// intent.setAction("android.intent.action.MAIN");
// intent.addCategory("android.intent.category.LAUNCHER");
// intent.setFlags(0x10200000);
// intent.setComponent(new ComponentName("com.android.camera",
// "com.android.camera.Camera"));
// startActivity(intent);
} @SuppressLint({ "SdCardPath", "SimpleDateFormat" })
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) { String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
Log.v("TestFile",
"SD card is not avaiable/writeable right now.");
return;
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");String name = format.format(new Date());fileName = "/sdcard/myImage/" + name + ".jpg";
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
FileOutputStream b = null; try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
imageView.setImageBitmap(bitmap);// 将图片显示在ImageView里
} }
}
上面获取到的拍摄照片的缩略图,要想获取到拍摄照片的原图,就要在打开相机的时候,把照片保存的地址必须设置好,代码如下:
package com.example.myphotos; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
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.ImageView; /**
* 2013-6-27 上午10:27:23
*
* @author 乔晓松
*/
public class CameraActivity extends Activity { private Button button;
private ImageView imageView;
private String fileName; @SuppressLint({ "SimpleDateFormat", "SdCardPath" })
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_camera);
button = (Button) findViewById(R.id.btn_camera);
imageView = (ImageView) findViewById(R.id.imageView1);
File file = new File("/sdcard/myImage/");
file.mkdirs();// 创建文件夹 button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH.mm.ss");
String name = format.format(new Date());
fileName = "/sdcard/myImage/" + name + ".jpg";
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(fileName)));
startActivityForResult(intent, 1);
}
});
// Intent intent = new Intent();
// intent.setAction("android.intent.action.MAIN");
// intent.addCategory("android.intent.category.LAUNCHER");
// intent.setFlags(0x10200000);
// intent.setComponent(new ComponentName("com.android.camera",
// "com.android.camera.Camera"));
// startActivity(intent);
} @SuppressLint({ "SdCardPath", "SimpleDateFormat" })
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) { String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
Log.v("TestFile",
"SD card is not avaiable/writeable right now.");
return;
} Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
FileOutputStream b = null; try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
imageView.setImageBitmap(bitmap);// 将图片显示在ImageView里
} }
}
布局文件代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/btn_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/btn_carema" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/btn_camera"
android:layout_below="@+id/btn_camera"
android:layout_marginTop="17dp"
android:background="#999999"
tools:ignore="ContentDescription" /> </RelativeLayout>
现在正在学习阶段,如有错误之处,请大牛们指导,我定修改此文章...
Java乔晓松-android中调用系统拍照功能并显示拍照的图片的更多相关文章
- Java乔晓松-android中上传图片到服务器Tomcat(Struts2)
在做android开发的时候,有时你会用到图片的上传功能,在我的android项目中,我是选中图片,点击上传多张图片 android客户端上传图片部分的代码如下: package com.exampl ...
- Java乔晓松-android中的帧动画FrameByFrame
先看效果后上代码: 动画开始---- 动画切换的界面---- 动画播放完毕后的跳转界面----- 重要的方法: imageView.setBackgroundResource(R.anim.frame ...
- Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内存溢出的Bug
由于android获取图片过大是会出现内存溢出的Bug 07-02 05:10:13.792: E/AndroidRuntime(6016): java.lang.OutOfMemoryError 解 ...
- Java乔晓松-android的四大组件之一Service(服务的绑定)
android的四大组件之一Service(服务的绑定) 怎么绑定服务,又怎么解除服务,代码如下: MainActivity.java源码: package com.example.lesson14_ ...
- Android中调用系统所装的软件打开文件(转)
Android中调用系统所装的软件打开文件(转) 在应用中如何调用系统所装的软件打开一个文件,这是我们经常碰到的问题,下面是我所用到的一种方法,和大家一起分享一下! 这个是打开文件的一个方法: /** ...
- Android中调用系统的相机和图库获取图片
//--------我的主布局文件------很简单---------------------------------<LinearLayout xmlns:android="http ...
- android中调用系统的发送短信、发送邮件、打电话功能
1 调用发送短信功能: Uri smsToUri = Uri.parse("smsto:"); Intent sendIntent = new Intent(Intent.ACT ...
- iphone开发中调用系统打电话功能
iphone开发中调用打电话功能,一般有2种: 1.系统的打电话代码,不返回当前程序: Java代码 [[UIApplication sharedApplication] openURL:[NSURL ...
- 关于android中调用系统拍照,返回图片是旋转90度
转载博客:http://blog.csdn.net/walker02/article/details/8211628 项目开发中遇到的一个问题,对于三星手机在做手机照片选择时出现图片显示不正常,研究后 ...
随机推荐
- Qt Windows下开机自动启动自己的程序
源地址:http://blog.csdn.net/chrisfxs/article/details/13279491 版权声明:本文为博主原创文章,未经博主允许不得转载. void Widget::R ...
- SilkTest Q&A 5
Q41.VerifyBitmap的问题. 我正在使用函数VerifyBitmap比较位置,边,颜色等,例如: Window.VerifyBitmap("Position.bmp", ...
- Automatic logon configuration on Linux OS
Automatic logon configuration on Linux OS 1. Regarding to DSA: a) ssh-keygen -t dsa b) cat ~/.ssh/i ...
- win7+vs2008+opencv
1.下载安装VS2008,然后直接下载opencv的windows的安装版, 2.把opencv解压出来,我的路径为:D:\Program\opencv 3.配置PATH:电脑--属性--高级系统设置 ...
- linux c socket 案源
service结束 #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #inclu ...
- a++为啥不能用作左值
原地址:http://wy892648414.blog.163.com/blog/static/212212135201378496591/ 1)首先说左值和右值的定义: 变量和文字常量都有存储区,并 ...
- CMake Intro - CMakeLists.txt
Notes: directory structure: cmake, cmake/Tutorial, cmake/Tutorial/MathLibs 1. File lists in cmake/ ...
- currentstyle和getComputedStyle兼容问题
currentStyle:获取计算后的样式,也叫当前样式.终于样式. 长处:能够获取元素的终于样式,包含浏览器的默认值,而不像style仅仅能获取行间样式,所以更经常使用到. 注意:不能获取复合样式如 ...
- VSTO学习笔记(一)VSTO概述
原文:VSTO学习笔记(一)VSTO概述 接触VSTO纯属偶然,前段时间因为忙于一个项目,在客户端Excel中制作一个插件,从远程服务器端(SharePoint Excel Services)上下载E ...
- Ubuntu——grub rescue 主引导修复
长期使用windows 和 ubuntu 人双系统,很可能遇到沉重的一个系统,或以其他方式加盟分区,导致系统重新启动时 : GRUB loading error:unknow filesystem g ...