微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用工作资料的设备无法打开相机

如何解决使用工作资料的设备无法打开相机

最近,我们的设备设置了工作资料。我们使用 Microsoft Intune 进行设备管理。

我们注意到,具有此工作资料设置的设备无法再使用其设备的相机或图库。没有设置工作资料的设备仍然可以使用这些功能

在应用程序中,我们通常会在需要时询问权限,但当通过工作配置文件安装应用程序时,不会询问这些权限。检查应用程序设置中的权限时,我们无法更改它们,但它表示已为相机和存储授予权限。我还查看了 https://developer.android.com/work/managed-profiles 上有关如何使用工作配置文件处理相机意图的来源。我们已经按照说明完全实现了这一点。

对于没有设置工作资料的设备,当我们拒绝相机和存储的权限时,我们可以重现该问题。然后行为完全相同。在具有需要使用相机的工作配置文件(例如 Outlook)的设备上使用不同的应用程序时,可以使用它并显示 Toast 消息,表明在工作配置文件之外使用了相机。

您将在下面找到我们用于启动相机意图的代码。我们有一个片段内部的活动:


public class CreateAttachmentActivity extends AppCompatActivity implements ColorChooserDialog.ColorCallback {
    private CreateAttachmentFragment createAttachmentFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_attachment);
        if (checkCameraPermissions()) {
            initView();
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocaleManager.onAttach(base,getCurrentUser().getPreferredLanguage()));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {
        if (requestCode == Constants.REQUEST_CAMERA_PERMISSIONS) {
            if (isPermissionsGranted(grantResults)) {
                initView();
            } else {
                Toast.makeText(getContext(),getContext().getString(R.string.camera_permission_not_granted),Toast.LENGTH_LONG).show();
                finish();
            }
        } else {
            Toast.makeText(getContext(),getContext().getString(R.string.wrong_permission_requested),Toast.LENGTH_LONG).show();
            finish();
        }
    }

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

    @Override
    public void onColorSelection(@NonNull ColorChooserDialog dialog,int selectedColor) {
        createAttachmentFragment.onColorSelection(dialog,selectedColor);
    }

    @Override
    public void onColorChooserdismissed(@NonNull ColorChooserDialog dialog) {
        createAttachmentFragment.onColorChooserdismissed(dialog);
    }


    private void initView() {
        this.createAttachmentFragment = CreateAttachmentFragment.newInstance();
        RippleView backButton = findViewById(R.id.backButton);
        getSupportFragmentManager().beginTransaction().add(R.id.pictureContainer,createAttachmentFragment).commit();
        backButton.setonRippleCompleteListener(rippleView -> onBackpressed());
    }

    private boolean checkCameraPermissions() {
        List<String> requiredPermissions = getrequiredPermissions();
        if (PermissionUtils.applicationHasPermissions(this,requiredPermissions)) {
            return true;
        } else {
            ActivityCompat.requestPermissions(this,new String[]{
                    Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},Constants.REQUEST_CAMERA_PERMISSIONS);
            return false;
        }
    }

    private static List<String> getrequiredPermissions() {
        return Arrays.asList(
                Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE
        );
    }
}

这是我们在fragment中用来打开相机视图的方法

private void openCameraview() {
        try {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File imageFile = AttachmentUtils.createImageFile(requireContext());
            if (intent.resolveActivity(requireActivity().getPackageManager()) != null) {
                this.imagePath = imageFile.getAbsolutePath();
                Uri uri = FileProvider.getUriForFile(requireContext(),requireContext().getPackageName().concat(Constants.AUTHORITY_FILEPROVIDER),imageFile);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
                intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setClipData(ClipData.newRawUri(null,uri));
                requireActivity().startActivityForResult(intent,Constants.INTENT_CAMERA);
            } else {
                Toast.makeText(getContext(),requireContext().getString(R.string.gallery_camera_not_opening_fallback),Toast.LENGTH_LONG).show();
            }
        } catch (IOException e) {
            Log.e(TAG,Log.getStackTraceString(e));
            onLoadImageError(true);
        }
    }

知道这个问题可能来自哪里吗?

亲切的问候

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。