Android 调用系统相机拍照

public class MainActivity extends Activity {
    private Button btn_bySysCamera, btn_bySysVideoCamera;
    ImageView iv_CameraImg;
    private Uri mCameraUri;
    File photoFile = null;
    String storagePath;
    String authority = "com.example.myapplication.fileprovider";

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

        btn_bySysCamera = (Button) findViewById(R.id.btn_bySysCamera);
        btn_bySysVideoCamera = (Button) findViewById(R.id.btn_bySysVideoCamera);
        iv_CameraImg = (ImageView) findViewById(R.id.iv_CameraImg);

        btn_bySysCamera.setOnClickListener(click);
        btn_bySysVideoCamera.setOnClickListener(click);
    }

    private View.OnClickListener click = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent captureIntent = null;
            switch (v.getId()) {
                case R.id.btn_bySysCamera:
                    captureIntent = new Intent();
//                    // 指定开启系统相机的Action
                    captureIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                    captureIntent.addCategory(Intent.CATEGORY_DEFAULT);

                    // 根据文件地址创建文件
                    Uri photoUri = null;
//        if (isAndroidQ) {
//            photoUri = createImageUri();  // 适配android 10
//        } else {
                    try {
                        photoFile = getFileFromCamera();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (photoFile != null) {
                        mCameraImagePath = photoFile.getAbsolutePath();
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//申请临时读取的权限
                            //适配Android 7.0文件权限,通过FileProvider创建一个content类型的Uri
                            photoUri = FileProvider.getUriForFile(MainActivity.this, authority, photoFile);
                        } else {
                            photoUri = Uri.fromFile(photoFile);
                        }
                    }
//        }
                    mCameraUri = photoUri;
                    if (photoUri != null) {
                        captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);//设置拍照后图片保存的位置
                        captureIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());// 设置图片保存的格式
                        captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    }
                    startActivityForResult(captureIntent, 1);

                    break;
//                case R.id.btn_bySysVideoCamera:
//                    intent = new Intent(MainActivity.this, SysVideoCameraActivity.class);
//                    startActivity(intent);
//                    break;
                default:
                    break;
            }

        }
    };

    /**
     * 用于保存图片的文件路径,Android 10以下使用图片路径访问图片
     */
    private String mCameraImagePath;

    private File getFileFromCamera() {
        File imageFile = null;
        File storageDir;
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        try {
            storagePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
            storageDir = new File(storagePath);
            storageDir.mkdirs();
            imageFile = File.createTempFile(timeStamp, ".jpg", storageDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imageFile;
    }

    Bitmap bitmap;

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {//点击确认
                if (photoFile != null && photoFile.exists()) {
                    try {
                        bitmap = MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), mCameraUri);//Uri转bitmap
                        MainActivity.this.runOnUiThread(new Runnable() {
                            public void run() {
                                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);//压缩
                                //把文件插入到系统图库
                                MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), bitmap, "Title", null);
                                Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCameraUri);
                                MainActivity.this.sendBroadcast(localIntent);
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
清单文件

    
xml布局文件


    

你可能感兴趣的:(移动开发)