使用意图在Android中使用相机

我正在使用下面的代码来使用意图使用相机。 在意图的参数我传递android.provider.MediaStore.ACTION_IMAGE_CAPTURE 。 它能够打开相机。 但问题是,它意外停止。 问题是它在OnActivityResults上给出空指针exception。 我已经使用了下面的代码:

 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); } } 

任何人都可以帮我解决这个问题吗?

Solutions Collecting From Web of "使用意图在Android中使用相机"

尝试请求代码1337。

 startActivityForResult(cameraIntent , 1337); 

这我怎么用它

 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, 1337); 

你的清单中是否有以下声明?

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

?? 我曾经这样做…这是我打电话给意图:

 File file = new File( _path ); Uri outputFileUri = Uri.fromFile( file ); Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE ); intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri ); intent.putExtra( MediaStore.EXTRA_VIDEO_QUALITY,1); 

我和你的代码之间的唯一的细微差别 – 我有呼叫之间的URI发送文件path

我已经使用了下面的代码,它的工作!

 @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); } } @Override protected void onActivityResult(int requestCode, int resultCode, final Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { im.setImageDrawable(null); im.destroyDrawingCache(); Bundle extras = data.getExtras(); Bitmap imagebitmap = (Bitmap) extras.get("data"); im.setImageBitmap(imagebitmap); } } 

使用意图在Android中使用相机

##

  Uri imageUri; 1:- TextView camera = (TextView)findViewById(R.id.camera); camera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File photo = new File(Environment.getExternalStorageDirectory(), new Date().getTime() + "myPic.jpg"); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); imageUri = Uri.fromFile(photo); startActivityForResult(cameraIntent, IMAGE_CAMERA_REQUEST);} 2:- @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try { if (requestCode == IMAGE_CAMERA_REQUEST && resultCode == Activity.RESULT_OK) { Uri selectedImage = imageUri; getActivity().getContentResolver().notifyChange(selectedImage, null); ContentResolver contentResolver = getActivity().getContentResolver(); Bitmap bitmap; try { bitmap = android.provider.MediaStore.Images.Media .getBitmap(contentResolver, selectedImage); imageDialog(bitmap); } catch (Exception e) { Log.e("Camera", e.toString()); } } } }} I hope it's working for you.