[Android:使用getPickImageChooserIntent时不是相机选项

问题描述 投票:0回答:1

我正在使用以下方法来调用选择文件的选项或单击我的onCreate方法中的图片:

startActivityForResult(getPickImageChooserIntent(this,"title",true,true), CODE_IMG_GALLERY)

但是,我在应用中看不到相机的选项。

enter image description here

以下是我的清单中的权限:

 <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <uses-feature android:name="android.hardware.camera" />
android android-camera
1个回答
0
投票

您需要为相机选项传递额外的意图对象。

    Intent pickIntent = new Intent();
    pickIntent.setType("image/*");
    pickIntent.setAction(Intent.ACTION_GET_CONTENT);

    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    String pickTitle = "Select or take a new Picture";
    Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
    chooserIntent.putExtra
            (
                    Intent.EXTRA_INITIAL_INTENTS,
                    new Intent[] { takePhotoIntent }
            );

    startActivityForResult(chooserIntent, SELECT_PICTURE);

然后您的onActivityResult获取相关数据。

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

    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            if (data != null) {
                InputStream inputStream;
                Bitmap bitmap = null;
                try {
                    if (data.getData() != null) {
                        inputStream = getContentResolver().openInputStream(data.getData());
                        bitmap = BitmapFactory.decodeStream(inputStream);
                    } else {
                        bitmap = (Bitmap) Objects.requireNonNull(data.getExtras()).get("data");
                    }

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.