简单的Andr​​oid摄像头应用程序 - 旋转导致NullPointerException异常 [英] Simple Android Camera App - Rotation causes NullPointerException

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

问题描述

我是新的Andr​​oid开发,并用相机玩弄。我只是想创建一个简单的应用程序,将拍摄照片时使用本机摄像头的应用程序,并给我回的图像文件的路径。

我有工作正常,但我已经打了一个奇怪的错误。当我轻点按钮来启动相机,如果我改变屏幕的方向,而在摄像头的应用程序,而不用切换回我退出相机(pressing完成按钮之前,当我问我想夺回与否),它会导致抛出NullPointerException。

我在一个有点不知所措的我在这里就如何计算这一个,所以任何信息将是有益的!

下面是code我到目前为止有:

 包com.CameraTest;

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

公共类CameraTestActivity扩展活动
{
    私有静态最终诠释CAMERA_PIC_REQUEST = 1234;
    保护乌里mCapturedImageURI;
    受保护的TextView TextView的;
    保护Button按钮;

    / **第一次创建活动时调用。 * /
    @覆盖公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        按钮=(按钮)findViewById(R.id.button1);
        TextView的=(的TextView)findViewById(R.id.textView1);

        如果(savedInstanceState!= NULL){
            textview.setText(savedInstanceState.getString(TextView中));
        }

        button.setOnClickListener(新View.OnClickListener()
        {
            公共无效的onClick(视图查看)
            {
                字符串文件名=temp.jpg;
                ContentValues​​值=新ContentValues​​();
                values​​.put(MediaStore.Images.Media.TITLE,文件名);
                mCapturedImageURI = getContentResolver()插入(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,值)。

                意向意图=新的意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,mCapturedImageURI);
                startActivityForResult(意向,CAMERA_PIC_REQUEST);
            }
        });
    }

    @覆盖保护无效的onSaveInstanceState(包outState)
    {
        super.onSaveInstanceState(outState);

        outState.putString(TextView的,textview.getText()的toString());
    }

    保护无效onActivityResult(INT申请code,INT结果code,意图数据)
    {
        super.onActivityResult(要求code,因此code,数据);

        如果(要求code == CAMERA_PIC_REQUEST和放大器;&安培;结果code == -1)
        {
            的String []投影= {MediaStore.Images.Media.DATA};
            光标光标= managedQuery(this.mCapturedImageURI,投影,NULL,NULL,NULL);
            INT column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            字符串capturedImageFilePath = cursor.getString(column_index_data);
            this.textview.setText(文件路径:+ capturedImageFilePath);
        }
    }
}
 

解决方案

你必须改变你的清单文件

在你的清单只需更换低于code

 <活动机器人:名称=。CameraTestActivity
              机器人:标签=@字符串/ APP_NAME机器人:configChanges =keyboardHidden |定位>
 

您code

 <活动机器人:名称=。CameraTestActivity
              机器人:标签=@字符串/ APP_NAME>
 

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.

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!

Here is the code I have so far:

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

with your code

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

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

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