gpt4 book ai didi

android - Android for Work 中的相机 Intent

转载 作者:太空狗 更新时间:2023-10-29 14:05:02 26 4
gpt4 key购买 nike

我正在触发相机 Intent 以使用相机应用程序拍照。它在我的应用程序的默认版本中按预期工作。

我已经为我的应用程序的 “Android for Work” 版本实现了相同的代码,但最终出现了奇怪的行为。通常,根据 “Android for Work” 视频,如果 Intent 在不同的配置文件中,则无法处理。例如,如果我的工作配置文件中没有安装相机应用程序,相机 Intent 在解析后应该为空,这是预期的。

但在我的例子中,Camera Intent 由安装在我的默认用户配置文件中的相机应用程序处理(Work Profile 没有相机应用程序)。当拍照时,它返回到 onActivityResult 并带有 resultCode Activity.RESULT_CANCELED

所以,我的问题是,如何区分用户的真实取消事件和这种情况?它们都有相同的结果,Activity.RESULT_CANCELED

下面的代码显示了我如何触发 Camera Intent。

/* Create Camera Intent */
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

/* Check if the Camera Intent is Available */
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) == null) {

/* Notify User in Error Case */
UIUtils.notifyUser(getActivity(), getString(R.string.error), getString(R.string.no_camera_application_found));

} else {

/* Create a new File with Random Photo Name */
takenPhoto = new File(FileUtils.getExternalPhotoCacheDirectory(), generatePhotoName());
takenPhoto.createNewFile();

/* Create Uri to send to within the Intent */
Uri outputFileUri = Uri.fromFile(takenPhoto);

/* Set Photo destination File Uri */
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

/* Start Activity for Result */
startActivityForResult(cameraIntent, ACTION_TAKE_PHOTO);
}

编辑:这是我的管理面板的一些屏幕截图。 Cross Profile Restrictions Applications Allowed in Work Profile

最佳答案

根据 setCameraDisabled() DevicePolicyManager 的方法:

Called by an application that is administering the device to disable all cameras on the device, for this user. After setting this, no applications running as this user will be able to access any cameras on the device.

因此,只有在工作配置文件上运行的应用程序将无法访问相机 - 正如它所说,这不会影响默认用户配置文件。

管理应用程序需要调用 addCrossProfileIntentFilter()FLAG_PARENT_CAN_ACCESS_MANAGED如果父(默认用户)应用不希望将任何 Intent 从工作资料发送到用户资料,则禁用来自工作资料应用的 Intent 能够在父(默认用户)应用上解析。

现在,如果您希望使用 ACTION_IMAGE_CAPTURE 的应用在工作资料上运行,您可以按照以下说明操作:

Android 上的每个用户都有一个完全沙盒化的存储空间,包括公共(public)存储目录。这意味着默认用户配置文件相机应用无法读取工作配置文件应用可访问的任何文件目录。

相反,您需要提供一个他们可以写入的 URI,例如 FileProvider 返回的 URI。如 file sharing training 中所述.

/* Create a new File with Random Photo Name */
takenPhoto = new File(getActivity().getCacheDir(), generatePhotoName());
takenPhoto.createNewFile();

/* Create Uri to send to within the Intent */
Uri outputFileUri = FileProvider.getUriForFile(getActivity(),
"your.authority.read.the.docs", takenPhoto);

/* Set Photo destination File Uri */
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

重要的是要注意,此处的文件将保存到您应用的缓存目录 - 而不是公共(public)存储位置。

您还需要包含基于 Uri 的权限:

cameraIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cameraIntent.setClipData(ClipData.newRawUri(null, outputFileUri));

通常,只有包含在 setData(uri) 中的 Uri 被授予权限,但剪辑数据中包含的任何 Uri 也将被授予权限,从而允许相机应用程序能够读取和写入你的 MediaStore.EXTRA_OUTPUT URI。

关于android - Android for Work 中的相机 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33178474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com