在前面的一篇文章中曾介绍过简单的开启相机照相功能,详见 Android简单调用相机Camera功能,实现打开照相功能 ,这一次就会将前面拍摄的照片显示到ImageView中,形成一个完整的效果

看实例

MainActivity.java

package com.example.camera;

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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; public class MainActivity extends Activity {
/** Called when the activity is first created. */
String SD_CARD_TEMP_DIR;
Bitmap myBitmap;
private ImageView imageView; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory()
+ File.separator + "tmp.jpg";//设定照相后保存的文件名称,相似于缓存文件 imageView = (ImageView)findViewById(R.id.imageView1);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(SD_CARD_TEMP_DIR)));
startActivityForResult(cameraIntent, 0);
}
});
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
Log.d("requestCode", "Need 0");
if(resultCode == RESULT_OK){
Log.d("resultCode", "OK!!!" + SD_CARD_TEMP_DIR);
myBitmap = BitmapFactory.decodeFile(SD_CARD_TEMP_DIR);
imageView.setImageBitmap(myBitmap);
}else{
Log.d("resultCode", "" + resultCode);
}
}else{
Log.d("requestCode", "Not Need");
}
}
}

看一下  activity_main.xml

<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"
tools:context=".MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Button" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" /> </RelativeLayout>

最好,再配置一下权限吧。看AndroidMainFest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.camera"
android:versionCode="1"
android:versionName="1.0" > <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission> <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.camera.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

这个地方,还要注意的是  android:hardwareAccelerated="false"  这一句,假设我们不加这一句的情况下。会非常easy出现下面问题

Bitmap too large to be uploaded into a texture

因此。我们须要将硬件加速功能关闭。

Android调用相机拍摄照片并显示到 ImageView控件中的更多相关文章

  1. [Android] 拍照、截图、保存并显示在ImageView控件中

    近期在做Android的项目,当中部分涉及到图像处理的内容.这里先讲述怎样调用Camera应用程序进行拍照,并截图和保存显示在ImageView控件中以及遇到的困难和解决方法.     PS:作者购买 ...

  2. Android 自己定义圆圈进度并显示百分比例控件(纯代码实现)

    首先,感谢公司能给我闲暇的时间,来稳固我的技术,让我不断的去探索研究,在此不胜感激. 先不说实现功能,上图看看效果 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZ ...

  3. Android调用相机实现拍照并裁剪图片,调用手机中的相冊图片并裁剪图片

    在 Android应用中,非常多时候我们须要实现上传图片,或者直接调用手机上的拍照功能拍照处理然后直接显示并上传功能,以下将讲述调用相机拍照处理图片然后显示和调用手机相冊中的图片处理然后显示的功能,要 ...

  4. Android 调用相机、相册功能

    清单文件中增加对应权限,动态申请权限(此部分请参考Android 动态申请权限,在此不作为重点描述) private static final int REQUEST_CODE_ALBUM = 100 ...

  5. Android控件之ImageView(显示图片的控件)

    一.ImageView属性: android:src = "@drawable/ic_launcher"——ImageView的内容图像(可以和android:background ...

  6. Pro Android 4 第六章 构建用户界面以及使用控件(一)

         目前为止,我们已经介绍了android的基础内容,但是还没开始接触用户界面(UI).本章我们将开始探讨用户界面和控件.我们先讨论一下android中UI设计的一般原理,然后我们在介绍一下an ...

  7. 《深入理解Android 卷III》第六章 深入理解控件(ViewRoot)系统

    <深入理解Android 卷III>即将公布,作者是张大伟.此书填补了深入理解Android Framework卷中的一个主要空白,即Android Framework中和UI相关的部分. ...

  8. web页面动态加载UserControl,并调用用户控件中的方法来初始化控件

    1,HTML页 头部注册: <%@ Register Src="~/WorkLog/WorkLogNewV1/UserControl/CeShiBu.ascx" TagPre ...

  9. OpenCV 2.2版本号以上显示图片到 MFC 的 Picture Control 控件中

    OpenCV 2.2 以及后面的版本号取消掉了 CvvImage.h 和CvvImage.cpp 两个文件,直接导致了苦逼的程序猿无法调用里面的显示函数来将图片显示到 MFC 的 Picture Co ...

随机推荐

  1. C/C++指针参数赋值问题

    今天遇到一个问题,即在C/C++中,关于在函数里对指针赋值的问题.首先可以看到如下现象: void test(int *p) { p = NULL; } int main(int argc, char ...

  2. python-memcached包使用方法

    本文转载自:http://my.oschina.net/flynewton/blog/10660 将memcached.pyc拷贝到工作目录 #!/usr/bin/env python import ...

  3. 正则表达式(特殊字符)/Xpath语法/CSS选择器

    正则表达式(特殊字符) ^ 开头 '^b.*'----以b开头的任意字符 $ 结尾 '^b.*3$'----以b开头,3结尾的任意字符 * 任意长度(次数),≥0 ? 非贪婪模式,非贪婪模式尽可能少的 ...

  4. linux系统下shell命令中的sleep

    在有的shell(比如linux中的bash)中sleep还支持睡眠(分,小时)sleep 1    睡眠1秒sleep 1s    睡眠1秒sleep 1m   睡眠1分sleep 1h   睡眠1 ...

  5. poj--1088--DFS(记忆化搜索之经典)

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 68057   Accepted: 25039 Description ...

  6. windows Server 2008 R2 IE增强安全配置正在阻止来自下列网站的内容

    1.在windows Server 2008 R2上访问百度,会出现以下界面 当在Windows Sever 2008 R2中运动IE8的时候会发现默认情况下IE启用了增强的安全配置,为了方便而且是在 ...

  7. 解决Matlab画图直接保存.eps格式而导致图不全的问题

    Matlab确是一款简单方便的工具,使用此工具绘图也是我们常用的一种手段,可是如果我们想将此图片保存成.eps格式并应用于latex中,就有可能会出现.eps格式的图片显示不全的问题,这个着实让我们头 ...

  8. iOS: 数据持久化方案

    数据持久化方案(如果总结不到位,或者有误的地方,敬请斧正) 一.功能: 主要是将数据持久化到本地,减少对网络请求的次数,既节省了用户的流量,也增强了App的体验效果. 二.种类:  plist存储:使 ...

  9. 【T04】开发并使用应用程序框架

    1.TCP/IP应用程序分为 TCP服务器 TCP客户端 UDP服务器 UDP客户端 2.构建框架库是比较简单的一件事,主要就是对socket编程.

  10. .NET Core Entity使用Entity Framework Core链接数据库

    首先安装Nuget包 Install-package Microsoft.EntityFrameworkCore Install-package Microsoft.EntityFrameworkCo ...