倾斜设备时相机应用程序开发和错误

时间:2014-05-02 07:33:31

标签: android android-intent

我按照教程https://www.youtube.com/watch?v=k-3zXb7GteU创建了我的相机应用程序,当我点击按钮时,我会让相机拍照。我有两个问题,

第一是当相机进来拍照时,如果我倾斜移动并拍照,我的应用程序就会停止工作。在某种意义上倾斜暴徒水平拍摄照片。

我想将这张照片连接到特定的数据库数据,比如特定的ID只存储这些照片。

我会在这里粘贴我的代码并请帮助我,我是新手。

mainActivity.java文件

`

private static String logCat= "Camera App";
private static int TAKE_PICTURE=1;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    Button cameraButton = (Button) findViewById(R.id.button_camera);
    cameraButton.setOnClickListener(cameraListener);

}
private OnClickListener cameraListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        takePhoto(v);
    }
};

private void takePhoto(View v){
    Intent intent =  new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"picture.jpg");
    imageUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intent, TAKE_PICTURE);

}
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);

    if(resultCode==Activity.RESULT_OK){
        Uri selectedImg = imageUri;
        getContentResolver().notifyChange(selectedImg, null);

        ImageView imageView = (ImageView) findViewById(R.id.image_id);
        ContentResolver cr = getContentResolver();
        Bitmap bitmap;

        try{

            bitmap = MediaStore.Images.Media.getBitmap(cr, selectedImg);
            imageView.setImageBitmap(bitmap);
            Toast.makeText(MainActivity.this, selectedImg.toString(), Toast.LENGTH_LONG).show();

        }catch(Exception e){
            Log.e(logCat, e.toString());
        }
    }
}`

activity_main.xml文件

`

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/container"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical"
     tools:context="com.example.camera.MainActivity" >


<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<ImageView
    android:id="@+id/image_id"
    android:background="#000000"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/image_cd_camera" />

 <Button
    android:id="@+id/button_camera"
    android:layout_width="132dp"
    android:layout_height="wrap_content"
    android:text="@string/button_camera_t" />

 </LinearLayout>`

错误日志

`

05-02 13:42:28.378: E/AndroidRuntime(328): FATAL EXCEPTION: main  
05-02 13:42:28.378: E/AndroidRuntime(328): java.lang.RuntimeException: Unable to resume   activity {com.example.camera/com.example.camera.MainActivity}: 
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1,   result=-1, data=Intent { act=inline-data dat=file:///storage/sdcard0/Pictures/picture.jpg typ=image/jpeg (has extras) }} to activity {com.example.camera/com.example.camera.MainActivity}: java.lang.NullPointerException

05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:258
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2609)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2092)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3518)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.access$700(ActivityThread.java:133)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.os.Looper.loop(Looper.java:137)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.main(ActivityThread.java:4803)
05-02 13:42:28.378: E/AndroidRuntime(328):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 13:42:28.378: E/AndroidRuntime(328):  at java.lang.reflect.Method.invoke(Method.java:511)
05-02 13:42:28.378: E/AndroidRuntime(328):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
05-02 13:42:28.378: E/AndroidRuntime(328):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
05-02 13:42:28.378: E/AndroidRuntime(328):  at dalvik.system.NativeStart.main(Native Method)
05-02 13:42:28.378: E/AndroidRuntime(328): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=file:///storage/sdcard0/Pictures/picture.jpg typ=image/jpeg (has extras) }} to activity {com.example.camera/com.example.camera.MainActivity}: java.lang.NullPointerException
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3147)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2568)
05-02 13:42:28.378: E/AndroidRuntime(328):  ... 13 more
05-02 13:42:28.378: E/AndroidRuntime(328): Caused by: java.lang.NullPointerException
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.os.Parcel.readException(Parcel.java:1431)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.os.Parcel.readException(Parcel.java:1379)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:452)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.content.ContentResolver.notifyChange(ContentResolver.java:1278)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.content.ContentResolver.notifyChange(ContentResolver.java:1257)
05-02 13:42:28.378: E/AndroidRuntime(328):  at com.example.camera.MainActivity.onActivityResult(MainActivity.java:69)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.Activity.dispatchActivityResult(Activity.java:5192)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3143)
05-02 13:42:28.378: E/AndroidRuntime(328):  ... 14 more`

1 个答案:

答案 0 :(得分:0)

将这些内容指定到您的清单......

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />

并在清单的主要活动标签中尝试这个....     机器人:configChanges =&#34;取向|屏幕尺寸| keyboardHidden&#34;

像这样......

<activity 
        android:name=".MainActivity" 
        android:label="@string/app_name" 
        android:configChanges="orientation|screenSize|keyboardHidden" > 
        <intent-filter> 
            <action android:name="android.intent.action.MAIN" /> 
            <category android:name="android.intent.category.LAUNCHER" /> 
        </intent-filter> 
    </activity>