Android打开系统相机并拍照的2种显示方法

本文实例为大家分享了Android打开系统相机并拍照的具体实现代码,供大家参考,具体内容如下

目标效果:

第二张为点击第一个按钮拍照后显示的,比较模糊,第三章为点击第二个按钮拍照后显示的,比较清楚。

1.activity_main.xml页面设置布局。

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/btnOpenCamera"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="10dp"
  android:text="拍照立即显示" />
 <Button
  android:id="@+id/btnSavePhoto"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="60dp"
  android:text="拍照存储后显示" />

 <ImageView
  android:id="@+id/ivShowPicture"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="130dp" />
</RelativeLayout>

2.MainActivity.java页面打开相机并获取传递回来的数据,两种方式第一个比较模糊,适合小图,第二个清楚些。

MainActivity.java页面:

package com.example.camera;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

 private Button btnOpenCamera, btnSavePhoto;
 private ImageView ivShowPicture;
 private static int REQUEST_CAMERA_1 = 1;
 private static int REQUEST_CAMERA_2 = 2;
 private String mFilePath;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 // 初始化控件
 init();

 // 控件绑定点击事件
 bindClick();
 }

 // 初始化控件和变量
 private void init() {
 btnOpenCamera = (Button) findViewById(R.id.btnOpenCamera);
 btnSavePhoto = (Button) findViewById(R.id.btnSavePhoto);
 ivShowPicture = (ImageView) findViewById(R.id.ivShowPicture);
 mFilePath = Environment.getExternalStorageDirectory().getPath();// 获取SD卡路径
 mFilePath = mFilePath + "/" + "temp.png";// 指定路径
 }

 // 控件绑定点击事件
 private void bindClick() {
 btnOpenCamera.setOnClickListener(this);
 btnSavePhoto.setOnClickListener(this);
 }

 @Override
 public void onClick(View view) {
 switch (view.getId()) {
 case R.id.btnOpenCamera:
 // 拍照并显示图片
 openCamera_1();
 break;
 case R.id.btnSavePhoto:
 // 拍照后存储并显示图片
 openCamera_2();
 break;
 default:
 break;
 }
 }

 // 拍照并显示图片
 private void openCamera_1() {
 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 启动系统相机
 startActivityForResult(intent, REQUEST_CAMERA_1);
 }

 // 拍照后存储并显示图片
 private void openCamera_2() {
 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 启动系统相机
 Uri photoUri = Uri.fromFile(new File(mFilePath)); // 传递路径
 intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);// 更改系统默认存储路径
 startActivityForResult(intent, REQUEST_CAMERA_2);
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 if (resultCode == RESULT_OK) { // 如果返回数据
 if (requestCode == REQUEST_CAMERA_1) { // 判断请求码是否为REQUEST_CAMERA,如果是代表是这个页面传过去的,需要进行获取
 Bundle bundle = data.getExtras(); // 从data中取出传递回来缩略图的信息,图片质量差,适合传递小图片
 Bitmap bitmap = (Bitmap) bundle.get("data"); // 将data中的信息流解析为Bitmap类型
 ivShowPicture.setImageBitmap(bitmap);// 显示图片
 } else if (requestCode == REQUEST_CAMERA_2) {
 FileInputStream fis = null;
 try {
  fis = new FileInputStream(mFilePath); // 根据路径获取数据
  Bitmap bitmap = BitmapFactory.decodeStream(fis);
  ivShowPicture.setImageBitmap(bitmap);// 显示图片
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 } finally {
  try {
  fis.close();// 关闭流
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 }
 }
 }
}

3.因为打开的是系统相机,所以不需要添加打开相机的权限,如果想要在别的应用里选择打开系统相机时也出现你的应用,需要在AndroidManifest.xml页面进行设置。

AndroidManifest.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-sdk
  android:minSdkVersion="17"
  android:targetSdkVersion="19" />

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 <application
  android:allowBackup="true"
  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>
   <!-- 注册相机功能,在别的程序Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);进行启动相机时也会选择是否启动该应用 -->
   <intent-filter>
    <action android:name="android.media.action.IMAGE_CAPTURE" />

    <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
  </activity>
 </application>

</manifest>

4.运行就可以显示目标效果了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android 系统相机拍照后相片无法在相册中显示解决办法

    Android 系统相机拍照后相片无法在相册中显示解决办法 目前自己使用发送广播实现了效果 public void photo() { Intent openCameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(openCameraIntent, TAKE_PICTURE); } 解决方法: protected void onActivityResul

  • Android自定义照相机倒计时拍照

    自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件: 两个TextView是用来显示提示信息和倒计时的秒数的 相关教程:Android开发从相机或相册获取图片裁剪 Android启动相机拍照并返回图片 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools&qu

  • android 7自定义相机预览及拍照功能

    本文实例为大家分享了Android实现摄像头切换,拍照及保存到相册,预览等功能,解决android7拍照之后不能连续预览的问题.参数设置相关问题以及前后摄像头语言颠倒等问题. import android.Manifest; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; imp

  • Android自定义组件获取本地图片和相机拍照图片

    iOS中有封装好的选择图片后长按出现动画删除效果,效果如下 而Android找了很久都没有找到有这样效果的第三方组件,最后懒得找了还是自己实现这效果吧 选择图片后还可对图片进行剪裁 当然,代码中还有很多不完善的地方,我接下来会继续完善这个组件的 已经上传到开源社区,欢迎大家来Star啊~ Demo源码:传送门 设计中的碰到的一些问题和解决思路 1.如何让加号图片显示在GridView最后面 首先在调用GridAdapter构造方法时就加载加号图片 /** * 图片适配器 * @param con

  • Android启动相机拍照并返回图片

    具体实现过程请看下面代码: 简单的调用了一下系统的拍照功能 代码如下所示: //拍照的方法 private void openTakePhoto(){ /** * 在启动拍照之前最好先判断一下sdcard是否可用 */ String state = Environment.getExternalStorageState(); //拿到sdcard是否可用的状态码 if (state.equals(Environment.MEDIA_MOUNTED)){ //如果可用 Intent intent

  • Android实现从本地图库/相机拍照后裁剪图片并设置头像

    玩qq或者是微信的盆友都知道,这些聊天工具里都要设置头像,一般情况下大家的解决办法是从本地图库选择图片或是从相机拍照,然后根据自己的喜爱截取图片.上述过程已经实现好了,最后一步我加上了把截取好的图片在保存到本地的操作,来保存头像.为了大家需要,下面我们小编把完整的代码贴出来供大家参考. 先给大家展示效果图: 代码部分: 布局代码(其实就是两个按钮和一个ImageView来显示头像) <LinearLayout xmlns:android="http://schemas.android.co

  • Android 实现调用系统照相机拍照和录像的功能

    本文实现android系统照相机的调用来拍照 项目的布局相当简单,只有一个Button: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_heig

  • Android使用系统自带的相机实现一键拍照功能

    今天分享的是用系统自带的相机实现一键拍照功能. public class MainActivity extends AppCompatActivity { private static final int TAKE_PHOTO = 100; private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setConte

  • Android自定义相机实现定时拍照功能

    这篇博客为大家介绍Android自定义相机,并且实现倒计时拍照功能. 首先自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件: activity_main.xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="m

  • Android如何调用系统相机拍照

    本文实例为大家分享了Android调用系统相机拍照的具体代码,供大家参考,具体内容如下 /** * 调用系统相机 */ private void takePhoto() { Uri uri = null; if (which_image == FRONT_IMAGE) { frontFile = new File(getSDPath() +"/test/front_" + getDate() + ".jpg"); uri = Uri.fromFile(frontFi

  • Android编程实现调用相册、相机及拍照后直接裁剪的方法

    本文实例讲述了Android编程实现调用相册.相机及拍照后直接裁剪的方法.分享给大家供大家参考,具体如下: package com.cvte.health.phone; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.content.ContentResolver; import android.co

  • Android调用系统照相机拍照与摄像的方法

    前言 在很多场景中,都需要用到摄像头去拍摄照片或视频,在照片或视频的基础之上进行处理.但是Android系统源码是开源的,很多设备厂商均可使用,并且定制比较混乱.一般而言,在需要用到摄像头拍照或摄像的时候,均会直接调用系统现有的相机应用,去进行拍照或摄像,我们只取它拍摄的结果进行处理,这样避免了不同设备的摄像头的一些细节问题.本篇博客将介绍在Android应用中,如何调用系统现有的相机应用去拍摄照片与短片,并对其进行处理,最后均会以一个简单的Demo来演示效果. 1.系统现有相机应用的调用 对于

  • Android 调用系统照相机拍照和录像

    本文实现android系统照相机的调用来拍照 项目的布局相当简单,只有一个Button: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_heig

随机推荐