0

In my application, I'm trying to make it so the User can press a button, which will allow them to take a picture using the stock camera application on their phone.

I am following the guide to using an external Camera app to capture images that I can use in my own app from the Android Developers Guide (http://developer.android.com/guide/topics/media/camera.html#intent-receive)

I'm having trouble with the onActivityResult() method, it apparently takes in 3 parameters

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if(resultCode == RESULT_OK) {
            Log.w("borre","Image saved to:\n" + data.getData());
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }
}

But at the moment, the data Intent is coming back as null, so calling any methods on the Intent parameter throws a NullPointerException

Here's the code I'm using to call up the Camera application (It's basically the same as the code in the guide)

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

Has anyone had this problem or knows why this Intent is coming back as null?

Cameron Wilby
  • 2,222
  • 1
  • 25
  • 35

1 Answers1

4

your are getting data part null bez you are not setting intent.setDataAndType() when you are starting Acitivty.like

    public static final String IMAGE_UNSPECIFIED = "image/*"; 
    Intent intent = new Intent(Intent.ACTION_PICK, null);  
    intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);  
    startActivityForResult(intent, 3);  

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == 0)  
       return; 
    if (requestCode == 2) {  
       Uri uri=data.getData();  //YOU GET DATA HERE
        }  
//OR
if (requestCode == 3) {  
 Bundle extras = data.getExtras();  
 if (extras != null) {  
 Bitmap photo = extras.getParcelable("data");  
 ByteArrayOutputStream stream = new ByteArrayOutputStream();  
 photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)????  
 imageView.setImageBitmap(photo);  
  }  
}  

or in your case getting image path use:

if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {  
     //pic path
      File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");  
   } 
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • This isn't the functionality I'm looking for, I want to the user to go straight to a camera, not to an image gallery. I'll see if I can modify this code to get the functionality I want though. +1 – Cameron Wilby Apr 07 '12 at 17:15
  • @cswilby : see your question you are asking about why Intent contain null ok and i answer you why it contain null.see my edit if you want to show image in your activity. – ρяσѕρєя K Apr 07 '12 at 17:20
  • or which functionality you want? – ρяσѕρєя K Apr 07 '12 at 17:22