调用摄像头和手机的相册
调用手机的摄像头和相机拍照的功能,在许多app中都非常常见,当我们用qq,微信,微博等app给别人分享图片时都会用到这个功能。
1,调用摄像头拍照
activity_main.xml布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="take photo"
/>
<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
- 设置一个button用来点击进行打开摄像头进行拍照。
- 设置一个imageView用来显示拍到的图片。
MainActivity.java代码
package com.example.chen.cameraalbumtest;
import android.Manifest;
import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 AppCompatActivity {
public static final int TAKE_PHOTO = 1;
private ImageView picture;
private Uri imageUri;
public static final int CHOOSE_PHOTO = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button takePhoto = (Button) findViewById(R.id.take_photo);
Button chooseAlbum = (Button)findViewById(R.id.choose_from_album);
picture = (ImageView) findViewById(R.id.picture);
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建File对象,用于存储拍照后的照片
File outputImage = new File(getExternalCacheDir(),"output_image.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if(Build.VERSION.SDK_INT >= 24) {
imageUri = FileProvider.getUriForFile(MainActivity.this,
"com.example.chen.cameraalbumtest.fileprovider" ,outputImage );
} else {
imageUri = Uri.fromFile(outputImage);
}
//启动相机
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri );
startActivityForResult(intent,TAKE_PHOTO );
}
});
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
if(resultCode == RESULT_OK) {
try {
//将拍摄的照片显示出来
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().
openInputStream(imageUri));
picture.setImageBitmap(bitmap);
} catch(FileNotFoundException e ) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
这里我们详细介绍一下button点击时间的逻辑:
首先,我们创建了一个File对象,用来存储拍摄的照片,把图片命名为 output_image.jpg,并将它放在手机SD卡的应用关联缓存目录。 应用关联缓存目录 是 sd卡专门用于存放当前应用缓存数据的位置,调用getExternalCacheDir()就可以得到这个目录。,具体在手机中的路径是 /sdcard/Android/data/包名/cache。 之所以是使用应用关联缓存目录是因为在Android 6.0时 读写SD卡被列入危险权限,如果将图片存放在sd卡其他目录,需要运行时权限。
接着,会对File对象进行处理,如果手机Android版本低于7.0,就调用 Uri的fromFile() 方法将File对象转为Uri对象。这个Uri对象标识着这张图片的本地真实路径。如果高于7.0,就会调用 FileProvider的getUriForFile() 方法,将File对象转换成一个包装过的Uri对象
getUriForFile()包含三个参数
- Context对象
- 任意唯一的字符串
- 刚刚创建的File对象
之所以需要这样的转换,是因为从Android 7.0 开始,直接使用本地真实路径的Uri被认为是不安全的,会抛出一个FileUriExposedException异常,FileProvider是一种特殊的内容提供器,它提供了类似内容提供器的机制对数据进行保护,可以选择性的将封装后的Uri分享给外部,提高应用的安全性。
再 **OnActivityResult()**函数中,如拍照成功,会调用BitmapFactory的decodeStream()方法将output_image.jpg 这张照片解析成Bitmap对象。再设置到imageView中显示出来。
在Manifest.xml中对provider进行注册
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chen.cameraalbumtest">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.chen.cameraalbumtest.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
</manifest>
在provider中,Android:name是固定的,android:authorities必须和getUriForFile()中的第二个参数相同。
imageUri = FileProvider.getUriForFile(
MainActivity.this,"com.example.chen.cameraalbumtest.fileprovider" ,outputImage );
另外,还要再meta-data标签中,指定Uri的共享路径,并引用@xml/file_paths资源,现在需要我们创建
xml文件中的external-path 就是用来指定Uri共享,name可以自己定义,path设置空置就表示整个sd卡进行共享。
在Android 4.4系统之前需要在AndroidManifest.xml中声明访问sd卡的权限
之后,我们就可以运行程序
2,从相册中选择照片
添加布局
<Button
android:id="@+id/choose_from_album"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose from albunm"
/>
MainActivity.java文件
package com.example.chen.cameraalbumtest;
import ...
public class MainActivity extends AppCompatActivity {
public static final int TAKE_PHOTO = 1;
private ImageView picture;
private Uri imageUri;
public static final int CHOOSE_PHOTO = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button takePhoto = (Button) findViewById(R.id.take_photo);
Button chooseAlbum = (Button)findViewById(R.id.choose_from_album);
picture = (ImageView) findViewById(R.id.picture);
//设置点击事件 打开相册
chooseAlbum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.
PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },1 );
} else {
openAlbum();
}
}
});
/**
* 打开相册
*/
private void openAlbum() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent,CHOOSE_PHOTO );
}
@Override
public void onRequestPermissionsResult(int requestCode,String[] permissions,
int[] grantResults) {
switch (requestCode) {
case 1:
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openAlbum();
} else {
Toast.makeText(this, "你没有权限", Toast.LENGTH_SHORT).show();
}
break;
default:break;
}
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
if(resultCode == RESULT_OK) {
try {
//将拍摄的照片显示出来
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().
openInputStream(imageUri));
picture.setImageBitmap(bitmap);
} catch(FileNotFoundException e ) {
e.printStackTrace();
}
}
break;
case CHOOSE_PHOTO:
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();
if (DocumentsContract.isDocumentUri(this, uri)) {
//如果是document类型的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".equalsIgnoreCase(uri.getScheme())) {
//如果是content类型的uri,则用普通方式处理
imagePath = getImagePath(uri, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
//如果是file类型的uri,直接获取图片路径
imagePath = uri.getPath();
}
displayImage(imagePath);
}
private void handleImageBeforeKitkat(Intent data) {
Uri uri = data.getData();
String 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();
}
}
}
注意,我们在点击事件中添加了申请运行时权限,WRITE_EXTERNAL_STORAGE这个危险权限,因为相册是存储在手机中的SD卡的,这个权限能够赋予我们对SD卡 读和写的能力。
当用户同意授权后,会调用openAlbum()方法,用来打开相册,指定intent的action为 ** android.intent.action.GET_CONTENT**,
再用startActivityForResult() 方法启动intent. 在 onActivityResult() 方法中,我们要对得到的图片进行处理 如果是4.4以上的系统会调用handleImageOnKitKat() 方法,4.4以下的系统会调用 handleImageBeforeKitKat() 方法。
现在我们运行程序…
选择图片