简单的 Android 相机应用程序 - 旋转导致 NullPointerException [英] Simple Android Camera App - Rotation causes NullPointerException

查看:12
本文介绍了简单的 Android 相机应用程序 - 旋转导致 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 开发新手,正在玩相机.我只是想创建一个简单的应用程序,该应用程序可以使用本机相机应用程序拍摄照片并将该图像的文件路径返回给我.

I'm new to Android development and was playing around with the camera. I just wanted to create a simple app that would take a photo using the native camera app and give me back the file path of that image.

我的工作正常,但我遇到了一个奇怪的错误.当我点击按钮启动相机时,如果我在相机应用程序中更改屏幕方向,并且在退出相机之前不要切换回来(当我被问及是否要重新拍摄时,按完成按钮与否),它会导致抛出 NullPointerException.

I have that working fine, but I've hit a strange error. When I tap the button to launch the camera, if I change the orientation of the screen while in the camera app, and don't switch back before I exit the camera (pressing the Done button when I'm asked if I want to retake or not), it causes a NullPointerException to be thrown.

对于如何解决这个问题,我有点不知所措,所以任何信息都会有所帮助!

I'm at a bit of a loss here as to how to figure this one out, so any information would be helpful!

这是我目前的代码:

package com.CameraTest;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CameraTestActivity extends Activity
{
    private static final int CAMERA_PIC_REQUEST = 1234;
    protected Uri mCapturedImageURI;
    protected TextView textview;
    protected Button button;

    /** Called when the activity is first created. */
    @Override public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button) findViewById(R.id.button1);
        textview = (TextView) findViewById(R.id.textView1);

        if (savedInstanceState != null) {
            textview.setText(savedInstanceState.getString("textview"));
        }

        button.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {               
                String fileName = "temp.jpg";  
                ContentValues values = new ContentValues();  
                values.put(MediaStore.Images.Media.TITLE, fileName);  
                mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
                intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);  
                startActivityForResult(intent, CAMERA_PIC_REQUEST);
            }
        });
    }

    @Override protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);

        outState.putString("textview", textview.getText().toString());
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == CAMERA_PIC_REQUEST && resultCode == -1)
        {
            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = managedQuery(this.mCapturedImageURI, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
            cursor.moveToFirst(); 
            String capturedImageFilePath = cursor.getString(column_index_data);
            this.textview.setText("File Path:" + capturedImageFilePath);
        }
    }    
}

推荐答案

你必须改变你的清单文件

you have to change your manifest file

在您的清单中只需替换以下代码

in your manifest just replace below code

<activity android:name=".CameraTestActivity"
              android:label="@string/app_name" android:configChanges="keyboardHidden|orientation">

使用您的代码

<activity android:name=".CameraTestActivity"
              android:label="@string/app_name">

这篇关于简单的 Android 相机应用程序 - 旋转导致 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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