繁体   English   中英

为什么摄像机意图无法返回到调用活动?

[英]Why does the camera intent not return to calling Activity?

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

目的是从这一行开始的。

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上进行测试。

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

您需要调用方法setResult

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

setResult(RESULT_OK, takePhotoIntent);

希望这个帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM