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中调用系统拍照功能并显示拍照的图片的更多相关文章

  1. Java乔晓松-android中上传图片到服务器Tomcat(Struts2)

    在做android开发的时候,有时你会用到图片的上传功能,在我的android项目中,我是选中图片,点击上传多张图片 android客户端上传图片部分的代码如下: package com.exampl ...

  2. Java乔晓松-android中的帧动画FrameByFrame

    先看效果后上代码: 动画开始---- 动画切换的界面---- 动画播放完毕后的跳转界面----- 重要的方法: imageView.setBackgroundResource(R.anim.frame ...

  3. Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内存溢出的Bug

    由于android获取图片过大是会出现内存溢出的Bug 07-02 05:10:13.792: E/AndroidRuntime(6016): java.lang.OutOfMemoryError 解 ...

  4. Java乔晓松-android的四大组件之一Service(服务的绑定)

    android的四大组件之一Service(服务的绑定) 怎么绑定服务,又怎么解除服务,代码如下: MainActivity.java源码: package com.example.lesson14_ ...

  5. Android中调用系统所装的软件打开文件(转)

    Android中调用系统所装的软件打开文件(转) 在应用中如何调用系统所装的软件打开一个文件,这是我们经常碰到的问题,下面是我所用到的一种方法,和大家一起分享一下! 这个是打开文件的一个方法: /** ...

  6. Android中调用系统的相机和图库获取图片

    //--------我的主布局文件------很简单---------------------------------<LinearLayout xmlns:android="http ...

  7. android中调用系统的发送短信、发送邮件、打电话功能

    1 调用发送短信功能: Uri smsToUri = Uri.parse("smsto:");  Intent sendIntent = new Intent(Intent.ACT ...

  8. iphone开发中调用系统打电话功能

    iphone开发中调用打电话功能,一般有2种: 1.系统的打电话代码,不返回当前程序: Java代码 [[UIApplication sharedApplication] openURL:[NSURL ...

  9. 关于android中调用系统拍照,返回图片是旋转90度

    转载博客:http://blog.csdn.net/walker02/article/details/8211628 项目开发中遇到的一个问题,对于三星手机在做手机照片选择时出现图片显示不正常,研究后 ...

随机推荐

  1. 如何得到动态链接库的输出函数tdump命令(225篇博文)

    有的时候,我们需要查看一个动态链接库的输出函数列表,有很多软件可以满足此要求,比如说 exeScope.不过,去下载一个软件总归是很麻烦,Delphi 本身就自带一个类似的工具,那就是 tdump.e ...

  2. Java.util.zip adding a new file overwrites entire jar?(转)

    ZIP and TAR fomats (and the old AR format) allow file append without a full rewrite. However: The Ja ...

  3. iOS 开发百问(6)

    61.警告"addexplicit braces to avoid dangling else" 所谓"危急的else"是相似这种代码: if(a== 10) ...

  4. Codeforces Round #350 (Div. 2)解题报告

    codeforces 670A. Holidays 题目链接: http://codeforces.com/contest/670/problem/A 题意: A. Holidays On the p ...

  5. [Android代码阅读]分类简介

    分类简介: 阅读他人的代码,可以学到很多东西,从思路,到方案,一系列都可以在项目代码中体现,所以,此分类专门用于记录阅读过的项目代码,并在上面给出自己的理解和注释 在此,感谢原作者开源分享项目代码

  6. How to convert `ctime` to `datetime` in Python? - Stack Overflow

    How to convert `ctime` to `datetime` in Python? - Stack Overflow How to convert `ctime` to `datetime ...

  7. FreeNAS 9.1.1 发布,网络存储系统 - 开源中国社区

    FreeNAS 9.1.1 发布,网络存储系统 - 开源中国社区 FreeNAS 9.1.1 发布,网络存储系统

  8. Xshell怎样登陆本地虚拟机

    Xshell怎样登陆本地虚拟机 本经验介绍了怎样使用Xshell登陆本地虚拟机,这里以centos为例.其实其它远程登陆,原理也是一样的.   工具/原料 VMware虚拟机 Xshell远程登陆工具 ...

  9. .net Mvc文件下载的功能,大文件下载完成之后修改数据库功能

    原文:.net Mvc文件下载的功能,大文件下载完成之后修改数据库功能 我服务器上文件只能下载一次,下载了之后就不能下载了,大文件或网速不好时,可能服务端文件流发送完了,客户端还没下载完,导致下载失败 ...

  10. 你不知道的JavaScript上卷笔记

    你不知道的JavaScript上卷笔记 前言 You don't know JavaScript是github上一个系列文章   初看到这一标题的时候,感觉怎么老外也搞标题党,用这种冲突性比较强的题目 ...