ACTION_IMAGE_CAPTURE onActivityResult具有空的意图和空的数据 [英] ACTION_IMAGE_CAPTURE onActivityResult has empty intent with empty data

查看:157
本文介绍了ACTION_IMAGE_CAPTURE onActivityResult具有空的意图和空的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读此文档后 https://developer.android.com/training/camera/photobasics 我想拍照,并将照片的uri存储到给定的变量中.问题是我将变量名称和uri用takePictureIntent.putExtra(Constants.INTENT_EXTRA_VARNAME, variable)takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)存储到意图中,但是在onActivityResult中,意图数据为空,并且存储的变量和uri都不为空.我需要知道uri和变量.我究竟做错了什么?

After reading this documentation https://developer.android.com/training/camera/photobasics I want to take a photo and to store the uri of the photo into a given variable. The problem is that I'm storing the variable name and the uri into the intent with takePictureIntent.putExtra(Constants.INTENT_EXTRA_VARNAME, variable) and takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI), but in onActivityResult the intent data is empty, and neither the variable and the uri stored are empty. I need to know the uri and the variable. What am I doing wrong?

我需要将信息作为意图数据传递,因为触发显示相机动作的类不同于活动类.

I need to pass the information as intent data because the class which is firing the action of displaying the camera is differente from the activity class.

带有操作的类:

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.putExtra(Constants.INTENT_EXTRA_VARNAME, variable);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(SectionManager.getInstance().getCurrentActivity().getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(SectionManager.getInstance().getCurrentActivity(), SectionManager.getInstance().getCurrentActivity().getPackageName(), photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            SectionManager.getInstance().getCurrentActivity().startActivityForResult(takePictureIntent, Constants.REQUEST_IMAGE_CAPTURE);
        }
    }

我收到结果的活动:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if( requestCode == Constants.REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
        String varName;
        String path;

        if(intent != null) {
            varName = intent.getStringExtra(Constants.INTENT_EXTRA_VARNAME);
            path = Util.getRealPathFromURI(SectionManager.getInstance().getCurrentActivity(), intent.getData());
            Log.d(DEBUG_TAG, "onActivityResult-> varName: "+varName+" path: "+path);
            if (varName != null && path != null) {
                VariableManager.put(varName, path);
            }
        }
    }
}

清单,权限write_external_storage和此代码:

manifest, permission write_external_storage and this code:

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>

file_paths.xml文件:

file_paths.xml file:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="/" />
</paths>

推荐答案

您已经具有与MediaStore.EXTRA_OUTPUT一起发送的photoURI,因此您可以简单地使用它.将photoURI另存为全局文件,然后直接使用它.看看是否发射了RESULT_OK,这意味着单击了图片并将其保存在EXTRA_OUTPUT位置.

You already have photoURI which you send with MediaStore.EXTRA_OUTPUT so You can simply use it . Save photoURI as globally and directly use it . See if RESULT_OK is emitted then it means picture was clicked and it will be saved at the EXTRA_OUTPUT location.

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if( requestCode == Constants.REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
        // USe photoURi here
    }
}

您不能指望某些Constants.INTENT_EXTRA_VARNAME自定义键会随系统相机意图一起返回.我对此事一无所知.
如果指定了MediaStore.EXTRA_OUTPUT,则拍摄的图像将被写入该路径,并且不会将任何数据提供给onActivityResult,因此您需要保留文件路径.

PS :您可以将其全局保存,并确保在onSaveInstanceState()期间将其持久保存.

You can not expect some custom key which is Constants.INTENT_EXTRA_VARNAME will be return with System camera Intent. I do not have any knowledge of such thing.
If you specified MediaStore.EXTRA_OUTPUT the image taken will be written to that path, and no data will given to onActivityResult so you need to persist the file path.

PS : You can save it globally and also make sure you should persist it during onSaveInstanceState().

这篇关于ACTION_IMAGE_CAPTURE onActivityResult具有空的意图和空的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆