演示代码的功能,调用摄像头拍照,然后返回把所拍的照片显示出来
1、首先呢,我们先创建一个项目,然后修改一下布局文件activity_main.xml,ImageView是将拍照的图片显示出来:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<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"/>
</RelativeLayout>
然后开始处理调用摄像头的逻辑,编写MainActivity
File对象用来存放拍下的图片,GetUriForFile()方法接受3个参数。第一个是要求传入的Context对象,第二个是任意唯一的字符串,第三个是刚刚创建的File对象,调用Intent的putExtra()方法指定图片的输出地址,最后调用StartActivityForResult()来启动活动。
package com.example.mrlee.camera;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
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 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;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button takephoto = findViewById(R.id.take_photo);
picture = findViewById(R.id.picture);
takephoto.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
File outputImage = new File(getExternalCacheDir(), "output_image.jpg");
try//判断图片是否存在,存在则删除在创建,不存在则直接创建
{
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//判断运行设备的系统版本是否低于Android7.0
if (Build.VERSION.SDK_INT >= 24)
{
imageUri = FileProvider.getUriForFile(MainActivity.this,
"com.example.cameraalbumtest.fileprovider", outputImage);
} else {
imageUri = Uri.fromFile(outputImage);
}
//使用隐示的Intent,调用摄像头,并把它存储
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
//调用会返回结果的开启方式,返回成功的话,则把它显示出来
}
});
}
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
//将图片解析成Bitmap对象,并把它显现出来
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
然后在AndroidMainfest.xml中队内容提供器进行注册
<provider
android:authorities="com.example.cameraalbumtest.fileprovider"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
@xml/file_paths资源还需要我们手动去创建,在res下创建一个xml目录,然后创建一个file_paths.xml文件, path的值表示共享的具体路劲,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="my_images"
path=""
/>
</paths>
最后呢我们还需要访问SD卡的权限,在AndroidMainfest.xml中加一行代码
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
这样就搞定啦,点击拍照,它会将拍下来的图片返回,然后通过ImageView显示出来。
具体细节请参见《第一行代码》