Android调用相机拍摄照片并显示到 ImageView控件中

在前面的一篇文章中曾介绍过简单的开启相机照相功能,详见?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

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

时间: 2024-08-06 10:16:00

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

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

最近在做Android的项目,其中部分涉及到图像处理的内容.这里先讲述如何调用Camera应用程序进行拍照,并截图和保存显示在ImageView控件中以及遇到的困难和解决方法. PS:作者购买了本<Android第一行代码 著:郭霖>,参照里面的内容完成(推荐该书,前面的布局及应用非常不错).网上这类资料非常多,作者仅仅分享给初学者同时在线记录些内容,希望对大家有所帮助. 首先,设置activity_main.xml为LinearLayout布局且 android:orientation=&q

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

首先,感谢公司能给我闲暇的时间,来稳固我的技术,让我不断的去探索研究,在此不胜感激. 先不说实现功能,上图看看效果 这个是续上一次水平变色进度条的有一个全新的控件,理论实现原理 1.分析控件:该控件基本上是圆圈内嵌圆圈: 2.进度计算:事实上是小学二年级数学题:当前进度/总数=百分比: 3.中间时间:呵呵,纯粹忽悠,不解释(当前时间). 理论总是和实践差距的太远.不扯淡.不吹嘘,贴代码: package com.spring.progressview; import java.text.Simp

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

在 Android应用中,非常多时候我们须要实现上传图片,或者直接调用手机上的拍照功能拍照处理然后直接显示并上传功能,以下将讲述调用相机拍照处理图片然后显示和调用手机相冊中的图片处理然后显示的功能,要想实现上传功能.一般都是上传到数据库中,将imageView中的图片取出来然后存到数据库中就可以. 以下讲述实现的步骤: 1. 调用相冊中的图片裁剪然后显示. 1.1 使用Intent获取从相冊中选择的照片. 1.2 对获取的图片进行裁剪处理.裁剪处理也是使用Intent调用的Android自带的裁

Android调用系统相册和相机选择图片并显示在imageview中

Android调用系统相册和相机选择图片并显示在imageview中,在系统调用相机拍摄中,直接返回的是经过压缩处理后的图像,当你直接把返还后的图片放在imageview中时 图片就会非常的模糊,所以要经过先存放在sd中,然后在处理并显示.当调用系统相册时,因为Android系统从4.4版本以后系统不再返回真实的uri路径,而是封装过后的uri路径,所以当你写代码时必须注意,4.4是一个分水岭,4.4以上的版本必须就通过解析和相应的处理才能获取到真实的uri路径. 先上程序运行的结果. 这个是调

win8 metro 调用摄像头拍摄照片并将照片保存在相应的位置

刚刚做过这类开发,所以就先献丑了,当然所贴上的源码都是经过验证过的,已经运行成功了,希望可以给大家一些借鉴: 下面是metro UI代码: <Page x:Class="Camera.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Android调用相机拍照获取原始照片

Android调用相机拍照,获取原始照片的解决方案如下:注意要有读取文件的权限,需要添加如下的权限:<uses-permission android:name="android.permission.CAMERA" />按钮点击事件: public void click(View view) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//相机捕捉图片的意图 intent.putExtra(Me

Android中显示进度的控件

显示进度的控件-------------------------ProgressBar:进度条核心属性包括:1) style:表示进度条的显示样式2) android:max:表示进度条的最大刻度3) android:progress:表示进度条当前的进度控制显示进度的方法:void setProgress(int progress)如果进度条没有配置为水平的进度条,则会显示为圆形进度条,这种圆形进度条没有进度的概念,通常用于例如关机.更新系统等时间完全不确定的.亦可以不需要告之用户进度的应用场

android控件之TextView(显示文本框控件)和EditText(输入文本框控件)

一.TextView(显示文本框控件) 1.TextView控件的常用属性 android:id——控件的id   android:layout_width——控件的宽度  android:layout_height——控件的高度 android:text——文本内容 android:textSize——文本大小 android:textColor——文本颜色 android:background——控件背景 <TextView android:id="@+id/name" an

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

首先,感谢公司能给我闲暇的时间,来稳固我的技术,让我不断的去探索研究,在此不胜感激. 先不说实现功能,上图看看效果 这个是续上一次水平变色进度条的有一个全新的控件,理论实现原理 1.分析控件:该控件基本上是圆圈内嵌圆圈: 2.进度计算:其实是小学二年级数学题:当前进度/总数=百分比: 3.中间时间:呵呵,纯粹忽悠,不解释(当前时间). 理论总是和实践差距的太远,不扯淡,不吹嘘,贴代码: package com.spring.progressview; import java.text.Simpl