【问题标题】:Why does the camera intent not return to calling Activity?为什么相机意图不返回调用 Activity?
【发布时间】:2018-01-20 22:33:11
【问题描述】:

我正在创建一个隐式的 android 意图。手机的相机应用程序打开。但是,当我拍照时,相机应用程序关闭,但启动相机意图的 Activity 未打开。手机进入主屏幕。如果我打开应用程序备份它仍然在相机应用程序中。我可以点击相机上的后退按钮并返回 Activity。

意图从这一行开始。

startActivityForResult(takePhotoIntent, IMAGE_REQUEST_CODE);

这是我正在创建的意图。

Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

然后我将 MediaStore.EXTRA_OUTPUT 添加到意图

Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePhotoIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException e) {
                // Error occurred while creating the File
                //TODO
                Log.e(TAG, e.toString());
                return null;
            }
            // Continue only if the File was successfully created
            if (photoFile != null && photoFile.exists()) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.markd.android.fileprovider",
                        photoFile);
                takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            } else {
                Log.e(TAG, "photoFile not configured");
            }
        } else {
            Log.e(TAG, "ResolveActivity is null");
        }
    }

这里是 createImageFile 方法。

private File createImageFile() throws IOException {
    File image = File.createTempFile(
            "home_image_" + UUID.randomUUID().toString(),  /* prefix */
            ".jpg",         /* suffix */
            getExternalFilesDir(Environment.DIRECTORY_PICTURES)      /* directory */
    );

    if(image.getParentFile().mkdirs()) {
        Log.e(TAG, "mkdirs:true");
    } else {
        Log.e(TAG, "mkdirs:false");
    }
    if(image.exists()) {
        Log.e(TAG, "Image exists");
        Log.e(TAG, "Path:"+image.getAbsolutePath());
    } else {
        Log.e(TAG, "Image does not exist");
    }

    // Save a file: path for use with ACTION_VIEW intents
    currentPhotoPath = image.getAbsolutePath();
    return image;
}

这是返回时应该调用的函数,但没有记录任何内容。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult");
    if (requestCode == IMAGE_REQUEST_CODE) {
        if(resultCode == Activity.RESULT_OK) {
            //Process result
        } else {
            Log.d(TAG, "Result not okay");
        }
    } else {
        Log.e(TAG, "Unknown Request");
    }
    super.onActivityResult(requestCode, resultCode, data);
}

它正在 Motorola XT1028 Android 5.1 API 22 上进行测试。

【问题讨论】:

  • 你覆盖了onActivityResult??
  • @Xenolion 编辑显示 onActivityResult 的 Override

标签: android android-intent android-activity android-camera-intent


【解决方案1】:

原来这是操作错误。代码是正确的。我正在按下主页按钮,这是一个软按钮,我认为它是拍照的按钮。我需要触摸屏幕才能拍照。这不是一个按钮。

【讨论】:

    【解决方案2】:

    你需要调用方法setResult

     Uri photoURI = FileProvider.getUriForFile(this,
                            "com.markd.android.fileprovider",
                            photoFile);
                    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
    
    setResult(RESULT_OK, takePhotoIntent);
    

    希望有帮助

    【讨论】:

    • 调用 setResult(RESULT_OK, takePhotoIntent) 没有帮助
    • 请在这里完全释放logcat
    • 您希望为我的应用程序过滤 logcat 吗?或者你想要没有过滤器?我认为没有过滤器,因为我相信它是相机应用程序。
    • 无过滤器。我想看看该程序是否没有超出预期的其他方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多