ACTION_IMAGE_CAPTURE onActivityResult has empty intent with empty data

丶灬走出姿态 提交于 2019-12-20 08:04:34

问题


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.

class with the action:

    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);
        }
    }

my activity which receives the result:

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);
            }
        }
    }
}

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:

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

回答1:


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
    }
}

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().



来源:https://stackoverflow.com/questions/50275714/action-image-capture-onactivityresult-has-empty-intent-with-empty-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!