gpt4 book ai didi

java - 相机 onActivityResult : resultCode is RESULT_CANCELED

转载 作者:行者123 更新时间:2023-12-01 18:40:56 25 4
gpt4 key购买 nike

我是 Java 和 Android 新手,但我有一个应用程序可以从相机拍摄照片并将其保存为文件。我可以启动相机并拍照,但在 onActivityResult 中,resultCode 始终为 RESULT_CANCELED (0)。首先,我遇到了 android.os.FileUriExposedException 错误,但我关注了此博客,问题似乎已解决:https://medium.com/@ali.muzaffar/what-is-android-os-fileuriexposedexception-and-what-you-can-do-about-it-70b9eb17c6d0

尽管我仍然有一个值为 0 的 resultCode (RESULT_CANCEL)。

下面是我启动相机 Activity 的代码:

private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");

Uri uri = FileProvider.getUriForFile(
this,
this.getApplicationContext()
.getPackageName() + ".provider", file);

intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
intent.addFlags(FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, uri, FLAG_GRANT_WRITE_URI_PERMISSION | FLAG_GRANT_READ_URI_PERMISSION);
}
}

startActivityForResult(intent, CAMERA_REQUEST_CODE);
}

下面是我的 onActivityResult (但 resultCode 始终为 0):

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Résultat de la capture de la photo
if (requestCode == CAMERA_REQUEST_CODE) {
if (resultCode == RESULT_OK) {

最佳答案

最后我按照此处给出的确切说明进行操作,https://developer.android.com/training/camera/photobasics ,现在可以了。

关于java - 相机 onActivityResult : resultCode is RESULT_CANCELED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59933440/

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