Android调用摄像头拍照s

一.首先修改main.xml文件

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/take_photo"
        android:text="拍照"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/picture"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
button用于拍照,ImageView显示拍好的照片.

二。实现主要逻辑部分

修改mainActivity的代码:

package com.example.cameraalbumtest;

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.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_PHOTP=1;
private ImageView imageView;
private Uri imageUri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button takephoto=(Button)findViewById(R.id.take_photo);
        imageView=(ImageView)findViewById(R.id.picture);
        takephoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                File outputImage=new File(getExternalCacheDir(),"output_image.jpg");
//这里创建一个file对象,用于存储拍摄的照片,把它存放在手机SDK的应用缓存目录下,用getExternalCacheDir方法可以获得这个目录。
从android6.0开始,读写SDK卡是危险权限,如果把图片放在SDK卡任何一个目录,运行时都要权限处理,使用应用缓存就不用了.
                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.cameraalbumtest.fileprovider",outputImage);
//如果系统版本高于android7.0,就调用这个方法吧file对象转换成Uri对象
                }
                else{
                    imageUri=Uri.fromFile(outputImage);
//如果系统版本低于android7.0,就调用Uri的fromfile把file对象转换成uri对象,这个URI是“.jpg”的真是路径.
                }
                Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
//指定照片的输出地址
                startActivityForResult(intent,TAKE_PHOTP);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case TAKE_PHOTP:
                if(resultCode==RESULT_OK){
                    try {
                        Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
//获得照片的uri,把它地址转换成Bitmap;
                        imageView.setImageBitmap(bitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }

                }
                break;
                default:
                    break;
        }

    }
}

需要在AndroidMainfest.xml文件中对provider注册

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.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:authorities="跟你前面填写的String一样"
            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>
    </application>

</manifest>

需要在res目录下创建 @xml/file_paths

的RES---->NEW------->DIRECTORY,创建一个xml文件,接着xml-------》new------》file------>file_paths.xml.

<?xml version="1.0" encoding="utf-8"?>
<paths >
    <external-path
        name="my_images"
        path=""/>

</paths>

为了兼容老版本手机,需要在访问SDK时声明权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

猜你喜欢

转载自blog.csdn.net/qq_36990613/article/details/80629996