以下为代码块:

package com.example.demo;

import java.io.File;

import android.annotation.TargetApi;

import android.app.Activity;

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.Build;

import android.os.Bundle;

import android.os.Environment;

import android.provider.DocumentsContract;

import android.provider.MediaStore;

import android.provider.MediaStore.Audio.Media;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.Toast;

public class MainActivity extends Activity {

public static final int TAKE_PHOTO = 1;

public static final int CROP_PHOTO = 2;

public static final int CHOOSE_PH = 3;

Button b, chooseFromAlbum;

ImageView pictrue;

Uri imageUri;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

b = (Button) findViewById(R.id.button1);

chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);

pictrue = (ImageView) findViewById(R.id.imageView1);

chooseFromAlbum.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent("android.intent.action.GET_CONTENT");

intent.setType("image/*");

startActivityForResult(intent,CHOOSE_PH);//打开相册。

}

});

b.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

File outputImage = new File(Environment

.getExternalStorageDirectory(), "output_image.jpg");// Environment.getExternalStorageDirectory()获取SD卡的根目录.

try {

if (outputImage.exists()) {

outputImage.delete();

}

outputImage.createNewFile();

} catch (Exception e) {

// TODO: handle exception

}

imageUri = Uri.fromFile(outputImage);

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);// 个人理解拍完放在imageUri下。

startActivityForResult(intent, TAKE_PHOTO);// 启动相机程序。

}

});

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {// requestCode:拍完照传回1,裁剪完传回2。

switch (requestCode) {

case TAKE_PHOTO:

if (resultCode == RESULT_OK) {

try {

Intent intent = new Intent("com.android.camera.action.CROP");

intent.setDataAndType(imageUri, "image/*");

intent.putExtra("scale", true);

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);// 剪完放在放在imageUri下。

startActivityForResult(intent, CROP_PHOTO);// 启动裁剪程序。

} catch (Exception e) {

Log.i("test", "e=" + e);

}

}

break;

case CROP_PHOTO:

if (resultCode == RESULT_OK) {

try {

Bitmap bitMap = BitmapFactory

.decodeStream(getContentResolver().openInputStream(

imageUri));// 解码。

Log.i("test", "bitMap=" + bitMap);

pictrue.setImageBitmap(bitMap);// 将裁减的图片显示出来。

} catch (Exception e) {

Log.i("test", "e1=" + e);

}

}

break;

case CHOOSE_PH:

if(resultCode == RESULT_OK ){

//判断当前系统版本。

if(Build.VERSION.SDK_INT>=19){

//

4.4以上的系统版本使用这个方法处理。

handleImageOnKitKat(data);

}else{

//

4.4以下的系统版本使用这个方法处理。

handleImageBeforeKitKat(data);

}

}

break;

default:

break;

}

}

@TargetApi(19)

private void handleImageOnKitKat(Intent data){

String imagePath = null;

Uri uri = data.getData();

Log.i("test","4.4以上uri="+uri);

if(DocumentsContract.isDocumentUri(this, uri)){

String docId = DocumentsContract.getDocumentId(uri);

if("com.android.providers.media.documents".equals(uri.getAuthority())){

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".equalsIgnoreCase(uri.getScheme())){

imagePath = getImagePath(uri,null);

}

//imagePath此时的路径为真实的手机图片的路径。

displayImage(imagePath);

}

private void handleImageBeforeKitKat(Intent data){

Uri uri = data.getData();

String imagePath = getImagePath(uri,null);

//imagePath此时的路径为真实的手机图片的路径。

displayImage(imagePath);

}

private String getImagePath(Uri uri,String selection){

String path = null;

//

getContentResolver:获得内容解析;query:查询。

Cursor cursor = getContentResolver().query(uri, null, selection, null, null);

if(cursor!=null){

if(cursor.moveToFirst()){

//

getColumnIndex:获得列索引。

path = cursor.getString(cursor.getColumnIndex(Media.DATA));

}

cursor.close();

}

return path;

}

private void displayImage(String imagePath){

if(imagePath!=null){

Bitmap bitmap = BitmapFactory.decodeFile(imagePath);

pictrue.setImageBitmap(bitmap);

}else{

Toast.makeText(this, "错误!", Toast.LENGTH_LONG).show();

}

}

}

以下为AndroidManifest.xml文件内容:

package="com.example.demo"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="14"

android:targetSdkVersion="19" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name=".MainActivity"

android:label="@string/app_name" >

以下为简单的布局文件:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.choosepictest.MainActivity" >

android:id="@+id/button1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:text="打开相机" />

android:id="@+id/imageView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button1"

android:layout_below="@+id/button1"

android:layout_marginTop="131dp" />

android:id="@+id/choose_from_album"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button1"

android:layout_below="@+id/button1"

android:layout_marginTop="26dp"

android:text="打开相册" />

android调用相册和摄像头,调用Android摄像头与打开相册相关推荐

  1. Android 11适配指南之系统相机拍照、打开相册,安卓app开发教程

    Android 6 权限适配 Android 7 文件适配 Android 10/11 存储适配 ok,接下来以一个更换头像的小例子来讲解一下. 示例 ======================== ...

  2. Android权限申请库——EasyPermissions使用详解和打开相册方法

    1.添加依赖 dependencies {implementation 'pub.devrel:easypermissions:3.0.0' } 2.在AndroidManifest文件中添加需要的权 ...

  3. Android Camera相机开发示例、Android 开发板 USB摄像头采集、定期拍照、定时拍照,安卓调用摄像头拍照、Android摄像头预览、监控,USB摄像头开发、摄像头监控代码

    我们有个需求,在机器上加个摄像头,定时拍照: 我到网上搜索,发现没有快速上手和简单使用的: 个人感觉,大部分博客写得很乱,或者长篇大论: 而我只想简单实现功能,并不打算学习多少理论: 下面代码是我写来 ...

  4. Android摄像头调用失败问题

    Android摄像头调用失败问题 问题描述: 之前在机顶盒上面对接视频会议APK时,发现第三方应用调用Camera.open()无法打开通过usb外接的摄像头. 定位分析: 我们通过阅读Android ...

  5. android调用虚拟摄像头方法,Android:如何在模拟器中使用摄像头?

    Android:如何在模拟器中使用摄像头? 通过在AVDpipe理器中将前置摄像头设置为"webcam0",我将一个networking摄像头连接到我的仿真器. 当我启动模拟器的相 ...

  6. Android 摄像头调用(不含拍照),kotlin开源

    mCurrentCamIndex = camIdx; //设置前置摄像头id }catch (RuntimeException e){ Log.e(TAG,"相机打开失败:" + ...

  7. Android开发之调用摄像头拍照(Android 第一行代码)

    布局文件(拍照按钮+图片显示) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xm ...

  8. Unity Android 打开相册和摄像头

    需求:要实现打开手机的相册和摄像头,选择照片或者拍照后,在unity进行. 1.android插件 我使用的是AndroidStuido来写插件,下面是一步步介绍流程 (1)创建android工程 注 ...

  9. Android调用系统的拍照,打开相册功能

    Android调用系统的拍照,打开相册功能 1 添加权限: <!-- 往SDCard写入数据权限 --> <uses-permission android:name="an ...

最新文章

  1. 你要的2019最全目标检测都在这里啦!
  2. 三周写出高性能的Python代码,这些小技巧你值得一试。
  3. php在线备忘录,一个会话备忘录小程序的实现方法
  4. java-判断相同和对象比较大小
  5. 使用的 SQL Server 版本不支持数据类型“datetime2”解决办法
  6. webview js 与 java 调用参数问题。
  7. 图解排序算法(三)之堆排序
  8. 如何清除图片下方出现几像素的空白间隙?
  9. red hat linux 改ip,Red Hat Enterprise Linux 7(RHEL7)配置静态IP地址
  10. 服务器 设置 将 Tomcat 注册 到系统服务 及使用方法
  11. myeclipse中hibernate出错
  12. JAVA 基础语法(一)——变量以及基本数据类型
  13. android读取主板数据恢复,重磅干货!高通9008模式与数据提取用于恢复数据
  14. Mount is denied because the NTFS volume is already exclusively opened.
  15. 最大数[抽象排序之抽象规则]
  16. 四十岁以后,如何做夫妻?
  17. 半年经验Java面试准备
  18. list.sort和list.stream.sorted
  19. Java重修之路(十)面向对象之多态详解,Object类,内部类,匿名内部类详解
  20. 你知道要去学人工智能,但你却无从入手,对吗?

热门文章

  1. python七大神级插件_IntelliJ IDEA 15款超级牛逼插件推荐(自用,超级牛逼)
  2. linux rpm找不到命令_Linux安装软件
  3. Udacity机器人软件工程师课程笔记(三十六) - GraphSLAM
  4. 【只需三步】用IDEA打开一个新的jsp项目如何跑起来(运行起来)
  5. PCT-36.523
  6. Clion 远程开发 配置
  7. 一文运维zookeeper
  8. DB-MySQL:MySQL 事务
  9. 「欧拉定理」学习笔记(费马小定理)
  10. 浅谈Android四大组件之Service