Junaid
Junaid

Reputation: 7860

Nullpointer exception while taking picture from camera

I am trying to capture a picture from camera (Samsung S3 specific problem) below is my code for the same:

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {  



    if( requestCode == 1337 && resultCode== RESULT_OK){
        Bundle extras = data.getExtras();
            if (extras != null){    
                   BitmapFactory.Options options = new BitmapFactory.Options();
            // options.inSampleSize = 1;
            // options.inPurgeable = true;
            // options.inInputShareable = true;
             thumbnail = (Bitmap) extras.get("data");
             image(thumbnail);
    }else{
                Toast.makeText(CreateProfile.this, "Picture NOt taken", Toast.LENGTH_LONG).show();
         } 

The image function:

     public void image(Bitmap thumbnail){
       Bitmap photo = thumbnail; 
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
       photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
       b = bos.toByteArray();
       ImageView imageview = (ImageView)findViewById(R.id.imageView1);
      }

Code for starting the camera intent:

       if(i==0){
                       Intent cameraIntent = new  Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                       startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 
                }

The Log Cat:

http://img534.imageshack.us/img534/8388/logcamera.png

http://img585.imageshack.us/img585/7559/camera1u.png

My code is working fine for HTC Wildfire S, Dell XCD35, Samsung Galaxy Grand and Samsung Galaxy Tab, clue less as to why this error is shown in S3. Any leads?

Upvotes: 2

Views: 1244

Answers (1)

Mr.Me
Mr.Me

Reputation: 9276

Well from your LOGCAT output it seems like the camera activity is returning a URI to the taken photo. and no thumbnail, just to make sure use:

if (extras.keySet().contains("data") ){
       thumbnail = (Bitmap) extras.get("data");
       image(thumbnail);
}

Regarding parsing the Uri In the intent you can use the following :

Uri imageURI = getIntent().getData();
ImageView imageview = (ImageView)findViewById(R.id.imageView1);
imageview.setImageURI(imageURI);

so the full code would look something like this :

    if (extras.keySet().contains("data") ){
       thumbnail = (Bitmap) extras.get("data");
       image(thumbnail);
} else {
    Uri imageURI = getIntent().getData();
    ImageView imageview = (ImageView)findViewById(R.id.imageView1);
    imageview.setImageURI(imageURI);
}

Upvotes: 2

Related Questions