Android调用摄像头和相册
点击打开链接
http://blog.csdn.net/u012796139/article/details/50084517
//layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.getphotoactivity.MainActivity"><Button android:id="@+id/take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="启动相机" /><Button android:id="@+id/choose_from_album" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="从相册中选择图片" /><ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /></LinearLayout> //Activity
package com.example.getphotoactivity;import android.annotation.TargetApi;import android.content.ContentUris;import android.content.Intent;import android.database.Cursor;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.DocumentsContract;import android.provider.MediaStore;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;public class MainActivity extends ActionBarActivity {private static final String TAG = "hdu";public static final int TAKE_PHOTO = 1;public static final int CROP_PHOTO = 2;public static final int CHOOSE_PHOTO = 3;private Button takePhoto;private ImageView picture;private Uri imageUri;private Button chooseFromAlbum;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main);takePhoto = (Button) findViewById(R.id.take_photo);picture = (ImageView) findViewById(R.id.picture);chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);takePhoto.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {// 创建File对象,用于存储拍摄后照片 File saveImage = new File(Environment.getExternalStorageDirectory(), "saveImage.jpg");try {if (saveImage.exists()) {saveImage.delete();}saveImage.createNewFile();}catch (IOException ex) {ex.printStackTrace();}imageUri = Uri.fromFile(saveImage);Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);// 启动相机 startActivityForResult(intent, TAKE_PHOTO);}});chooseFromAlbum.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {Intent intent = new Intent("android.intent.action.GET_CONTENT");intent.setType("image/*");// 打开相册 startActivityForResult(intent, CHOOSE_PHOTO);}});}@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (requestCode) {case TAKE_PHOTO:if (resultCode == RESULT_OK) {Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(imageUri, "image/*");intent.putExtra("scale", true);intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);// 启动裁剪程序 startActivityForResult(intent, CROP_PHOTO);}break;case CROP_PHOTO:if (resultCode == RESULT_OK) {try {Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));// 显示裁剪后的图片 picture.setImageBitmap(bitmap);}catch (FileNotFoundException ex) {ex.printStackTrace();}}break;case CHOOSE_PHOTO:if (resultCode == RESULT_OK) {handleImage(data);}break;default:break;}}// 只在Android4.4及以上版本使用 @TargetApi(19)private void handleImage(Intent data) {String imagePath = null;Uri uri = data.getData();if (DocumentsContract.isDocumentUri(this, uri)) {// 通过document id来处理 String docId = DocumentsContract.getDocumentId(uri);if ("com.android.providers.media.documents".equals(uri.getAuthority())) {// 解析出数字id String id = docId.split(":")[1];String selection = MediaStore.Images.Media._ID + "=" + id;imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);}else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId));imagePath = getImagePath(contentUri, null);}}else if ("content".equals(uri.getScheme())) {// 如果不是document类型的Uri,则使用普通方式处理 imagePath = getImagePath(uri, null);}// 根据图片路径显示图片 displayImage(imagePath);}private String getImagePath(Uri uri, String selection) {String path = null;// 通过Uri和selection来获取真实图片路径 Cursor cursor = getContentResolver().query(uri, null, selection, null, null);if (cursor != null) {if (cursor.moveToFirst()) {path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));}cursor.close();}return path;}private void displayImage(String imagePath) {if (imagePath != null) {Bitmap bitmap = BitmapFactory.decodeFile(imagePath);picture.setImageBitmap(bitmap);}else {Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show();}} }
Android调用摄像头和相册相关推荐
- android调用相册和摄像头,Android8.3调用摄像头和相册
我们平时在使用QQ或微信的时候经常要和别人分享图片,这些图片可以是用手机摄像头拍的,也可以是从相册中选取的.类似这样的功能实在是太常见了,几乎在每个应用程序中都会有,那么本节我们就学习一下调用摄像头和 ...
- Android基础实战之调用摄像头与相册 | 带实例
调用摄像头与相册 调用摄像头拍照/从相册选择照片 xml 设置了两个按钮,分别是打开摄像头的按钮,以及打开相册的按钮 <Buttonandroid:id="@+id/take_phot ...
- android 7调用摄像头,Android调用摄像头拍照(兼容7.0)
[实例简介] Android调用摄像头拍照(兼容7.0)Demo,原博客文章https://blog.csdn.net/u010356768/article/details/70808162 [实例截 ...
- Android调用相机与相册的方法
Android调用相机与相册的方法 操作流程 点击拍摄 效果图 点击相册选择 效果图 拒绝权限的情况 效果图 功能实现 项目配置 1.先在项目根目录的build.gradle的repositories ...
- Android调用摄像头--农民伯伯原文
Android开发指南(33) -- Multimedia and Camera - Camera 前言 本章内容为Android开发者指南的 Framework Topics/Multimedia ...
- Android 使用摄像头和相册
1.调用摄像头拍照 新建一个CameraAlbumTest项目,然后修改activity_main.xml <LinearLayout xmlns:android="http://sc ...
- android: 调用摄像头拍照
很多应用程序都可能会使用到调用摄像头拍照的功能,比如说程序里需要上传一张图片 作为用户的头像,这时打开摄像头拍张照是最简单快捷的.下面就让我们通过一个例子来学 习一下,如何才能在应用程序里调用手机的摄 ...
- android 相机和相册,[转载][转载] android调用相机和相册
很多同学在做客户端的时候,都需要做上传图片,那么上传图片如何上传呢?自己写一个SurfaceView做为展示区,然后用camera类来实现?太out了!直接调用相机拍照获取或者调用相册来取照片才是王道 ...
- android摄像头代码,android: 调用摄像头拍照(示例代码)
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CROP_PHOTO); //启动 ...
最新文章
- javascript专业八级测试答案整理
- 【机器学习】干货!机器学习中 5 种必知必会的回归算法!
- 简单排序算法设计(Java)
- 树莓派跑php,在树莓派4上部署nginx+php
- jq ajax异步上传文件,jQuery插件ajaxFileUpload异步上传文件
- 火狐浏览器账号登录步骤详解
- 这是小小本周的第六篇,本篇小小将会介绍一个很古老很古老很古老的为什么系列之不能重写service方法。...
- 如何查看电脑是几核几线程
- 在线2-36任意进制转换工具
- k进制正整数的对k-1取余与按位取余
- VB语言复习助力(基础篇)
- Ubuntu18.04版本安装ssh及连接ssh的常见问题
- PHP短视频无水印解析源码
- iOS面试题(多线程篇)
- 银联支付接口申请流程-傲付宝
- 还记得那年大明湖畔的Java 7吗
- PX4 FMU启动流程 1.nsh
- Head First Design Mode(2)-设计模式入门(策略模式)
- 论文阅读笔记《PoseCNN: A Convolutional Neural Network for 6D Object Pose Estimation in Cluttered Scenes》
- 计算机软件系统的组成