摄像头活动造成的URI去空当屏幕方向变化 [英] Camera activity causing uri to go to null when screen orientation changes

查看:104
本文介绍了摄像头活动造成的URI去空当屏幕方向变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置一个数据成员,imageUri,并将其传递到该启动摄像头活动的意图。在摄像头的活动我拍张照片的点击复选框回到我的活动之前,旋转屏幕。当我这样做,imageUri为null,则onActivityResult被调用。如果我没有旋转屏幕的一切工作就好了imageUri不为空。

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没有歌厅叫我的活动,这样不是问题。

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?

感谢

推荐答案

我相信imageUri为您的活动现场,对不对?如果您旋转设备的活动被破坏并重新启动,你的字段为空。您必须将URI保存为您的活动状态的一部分。有几种可能的方法来实现这一点,就是使用的onSaveInstanceState(),在这里看到更多的细节:<一href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" rel="nofollow">http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29

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

这篇关于摄像头活动造成的URI去空当屏幕方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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