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

所属分类: 软件编程 / Android 阅读数: 75
收藏 0 赞 0 分享

本文实例为大家分享了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.运行就可以显示目标效果了。

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

更多精彩内容其他人还在看

Android框架学习之Volley和Glide详解

这篇文章主要给大家介绍了关于Android框架学习之Volley和Glide的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android中Fragment的基本用法示例总结

Fragment是activity的界面中的一部分或一种行为,下面这篇文章主要给大家介绍了关于Android中Fragment的基本用法的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
收藏 0 赞 0 分享

Android.mk引入第三方jar包和so库文件的方法

这篇文章主要介绍了Android.mk引入第三方jar包和so库文件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿微信录制小视频

这篇文章主要为大家详细介绍了Android仿微信录制小视频,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

android实现一键锁屏和一键卸载的方法实例

这篇文章主要给大家介绍了关于android如何实现一键锁屏和一键卸载的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

Android手势密码--设置和校验功能的实现代码

这篇文章主要介绍了Android手势密码--设置和校验功能的实现代码,非常不错,具有一定的参考校验价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Kotlin学习笔记之const val与val

这篇文章主要给大家介绍了关于Kotlin学习笔记之const val与val的相关资料,并给大家介绍了const val和val区别以及Kotlin中var和val的区别,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android实现调用系统分享功能示例的总结

这篇文章主要介绍了通过Android调用系统分享文本信息、单张图片、多个文件和指定分享到微信、QQ,同时分享图片和文字的功能示例,小编觉得挺不错,一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android自定义view实现输入控件

这篇文章主要为大家详细介绍了Android自定义view实现输入控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码

这篇文章主要介绍了Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码的实例代码,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多