使用意图使用相机在安卓意图、相机

2023-09-06 03:05:31 作者:淡笑、此时的颓废

我使用下面的code。通过使用意图使用相机。 在意向中的参数我传递 android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE 。 它是能够打开相机。 但问题是,它意外停止。 现在的问题是,它给空指针异常的 OnActivityResults 。 我已经使用了低于code:

I am using the following code to use camera by using intent. In the parameter of intent I am passing android.provider.MediaStore.ACTION_IMAGE_CAPTURE. It is able to open the camera. But the problem is that it stops unexpectedly. The problem is that it gives null pointer exception on OnActivityResults. I have used the below code:

public class demo extends Activity {

Button ButtonClick;
int CAMERA_PIC_REQUEST = 2; 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ButtonClick =(Button) findViewById(R.id.Camera);
    ButtonClick.setOnClickListener(new OnClickListener (){
        @Override
        public void onClick(View view)
        {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            // request code

            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if( requestCode == CAMERA_PIC_REQUEST)
    {
    //  data.getExtras()
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
        image.setImageBitmap(thumbnail);
    }
    else 
    {
        Toast.makeText(demo.this, "Picture NOt taken", Toast.LENGTH_LONG);
    }
    super.onActivityResult(requestCode, resultCode, data);
}
}

谁能帮我解决这个问题?

Can anyone help me to solve this problem?

推荐答案

尝试请求code 1337。

Try request code 1337.

startActivityForResult(cameraIntent , 1337);