zhihu / Matisse

:fireworks: A well-designed local image and video selector for Android
Apache License 2.0
12.52k stars 2.06k forks source link

如何直接调用拍照 #758

Open AndroidLMY opened 4 years ago

AndroidLMY commented 4 years ago

不通过相册里面的拍照 如何外部代码调用相机拍宅

lydlovezjr commented 4 years ago

同问

Tkorn commented 4 years ago
    MediaStoreCompat mMediaStoreCompat = new MediaStoreCompat(this);
    mMediaStoreCompat.setCaptureStrategy(new CaptureStrategy(true,getPackageName()+".fileprovider"));
    mMediaStoreCompat.dispatchCaptureIntent(this,CALL_CAMERA_ACTIVITY_REQUEST_CODE);

源码是这样的

AndroidLMY commented 4 years ago

我这样写了 能够调用相机拍照但是返回回调到onActivityResult Intent data这个回调参数为空 不知道什么原因

Tkorn commented 4 years ago

用这个就可以 mMediaStoreCompat.getCurrentPhotoUri()

MJ9306 commented 4 years ago
    File dir = new File(Environment.getExternalStorageDirectory(), "pictures");
                    if (dir.exists()) {
                        dir.mkdirs();
                    }
                    currentImageFile = new File(dir, System.currentTimeMillis() + ".webp");
                    if (!currentImageFile.exists()) {
                        try {
                            currentImageFile.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(currentImageFile));
                    startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER);