0

这是文件的内容:MainActivity.xml

package com.example.camera_test;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener()
        {
        @SuppressLint("NewApi") @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
            int i = Camera.getNumberOfCameras();
            TextView age = (TextView) findViewById(R.id.textView1);
            age.setText(i);
            startActivityForResult( intent, 0 );
        }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

效果很好,我点击了按钮,它在我的设备上打开了相机应用程序!但是一旦我添加了这些行:

int i = Camera.getNumberOfCameras();
            TextView age = (TextView) findViewById(R.id.textView1);
            age.setText(i);

我在我的设备上收到错误说 nned 强制关闭。

我也尝试过 age.setText(I); 这个:

age.setText(Integer.toString(i));

我试过而不是 id.textView1 也 id.button1 但没有工作。

这是 activity_main.xml 文件的内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="45dp"
        android:layout_marginTop="62dp"
        android:text="Activate The Camera" />

</RelativeLayout>

这是我唯一更改的两个文件。

4

1 回答 1

0

Solution. Now when I click the button I see the camera then take a photo click on Done then I see the image I took on a small window in my device !

This is the MainActivity.Java fileL:

package com.example.camera_test;

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private static final int CAMERA_PIC_REQUEST = 1337; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener()
        {
        @SuppressLint("NewApi") @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );

            startActivityForResult( intent, CAMERA_PIC_REQUEST );
        }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_PIC_REQUEST) {  
        // do something
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ImageView image = (ImageView) findViewById(R.id.imageView1);
            image.setImageBitmap(thumbnail);
        }  
       }  
}

And added to the activity_main.xml file imageView1 in the bottom:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="45dp"
        android:layout_marginTop="62dp"
        android:text="Activate The Camera" />
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>

Now it's all working ! Thanks.

于 2013-01-19T23:48:01.693 回答