安卓调用系统相机拍照并且显示在ImageView上

时间:2022-10-13 22:44:44

并没有什么技术难点,只是在保存到sdCard的时候有一点小细节需要注意,所以写了这篇文章。代码很简单,就不解释什么了,直接贴上源码。

public class TakePhotoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_photo);

        ((Button) findViewById(R.id.takephoto)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(intent, 1);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK){
            String sdStatus = Environment.getExternalStorageState();

            if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){
                System.out.println(" ------------- sd card is not avaiable ---------------");
                return;
            }


            String name = "photo.jpg";

            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");

            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/");
            file.mkdirs(); //创建文件夹

            String fileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+name;

            FileOutputStream b =null;

            try {
                b=new FileOutputStream(fileName);
                bitmap.compress(Bitmap.CompressFormat.JPEG,100,b);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally {
                try {
                    b.flush();
                    b.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            ((ImageView) findViewById(R.id.picture)).setImageBitmap(bitmap);

        }
    }
}



布局文件一个Button、一个ImageView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_take_photo"
    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.zhuandian.eventbus.takephoto.TakePhotoActivity">


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="take photo"
        android:id="@+id/takephoto"
        />
    <ImageView
        android:layout_below="@+id/takephoto"
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:id="@+id/picture"
        />
</RelativeLayout>

由于调用系统相机需要给权限,不要忘了在manifest文件里面给权限声明

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

    <uses-feature android:name="android.hardware.camera" />

    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />