Camera activity causing uri to go to null when scr

2019-01-28 04:29发布

问题:

I set a data member, imageUri, and pass that into an intent that launches the camera activity. In the camera activity I take a picture and the rotate the screen before clicking the check box to return to my activity. When I do this, imageUri is null when onActivityResult is called. If I don't rotate the screen everything works just fine and imageUri is not null.

onConfigurationChanged is not geting called in my activity so that is not the issue.

public void takePhoto() {
    //define the file-name to save photo taken by Camera activity
    fileName = getFileNameDate();
    //create parameters for Intent with filename
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");

    imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

    //create new Intent
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    startActivityForResult(intent, picture_result_code);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i("Camera_onActivityResult", "Got activity result requestCode = " + requestCode + " resultCode: " + resultCode);
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case picture_result_code:
        if (resultCode == Activity.RESULT_OK) {
            try {
                Log.i("Camera", "Preparing to upload image...");

                picFile = convertImageUriToFile(imageUri);  // Here imageUri is null and causing crash

                uploadFile(picFile.getPath());

            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
                Log.e("Camera", e.toString());
                e.printStackTrace();
            }
        }
    }
}

Is there some other way that I should be retrieving the image?

Thanks

回答1:

I believe imageUri is a field in your activity, right? if you rotate the device the activity is destroyed and restarted, and your field is null. You have to save the URI as part of the state of your activity. There are a few possible methods to achieve that, on is to use onSaveInstanceState(), see here for more details: http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29



回答2:

Locking the screen orientation of the invoking Activity fixed the issue for me. If you don't want to lock yours, you can create an Activity with locked orientation just for calling the camera, then returning the result to your 'real' activity.

To lock the screen orientation, put this into the activity definition in your manifest (both lines):

android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize"

"portrait" can also be "landscape"