Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android crashing after camera Intent

I have an app published and one of the fundamental features is to allow the user to take a picture, and then save that photo in a specific folder on their External Storage.

Everything seems to be working fine, but I've gotten two reports now that claim that after taking a photo, and clicking "Done" to exit the camera (and return back to the Activity), the app is Forced Closed, bringing the user back to the home screen.

This happens on a Samsung Nexus S and the Galaxy Tab. Below I've posted my code to show I set up my intent and how I handle saving and displaying the photo in onActivityResult(). Any guidance on what might be causing it to crash after they click "Done" to exit the camera app, would be greatly appreciated!

Again, this seems to be working fine on most devices but I was wondering if their is a more efficient, universal approach I should be taking. Thank you

How I'm firing the Camera Intent

   case ACTION_BAR_CAMERA:          // numbered image name         fileName = "image_" + String.valueOf(numImages) + ".jpg";           output = new File(direct + File.separator + fileName); // create                                                                     // output         while (output.exists()) { // while the file exists             numImages++; // increment number of images             fileName = "image_" + String.valueOf(numImages) + ".jpg";             output = new File(outputFolder, fileName);           }         camera = new   Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);         uriSavedImage = Uri.fromFile(output); // get Uri of the output         camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); //pass in Uri to camera intent         startActivityForResult(camera, 1);           break;     default:         return super.onHandleActionBarItemClick(item, position);     }     return true; } 

How I'm setting up onActivityResult()

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {     // TODO Auto-generated method stub     super.onActivityResult(requestCode, resultCode, data);      if (resultCode == RESULT_OK) { // If data was passed successfully          Bundle extras = data.getExtras();          //Bundle extras = data.getBundleExtra(MediaStore.EXTRA_OUTPUT);          /*ad = new AlertDialog.Builder(this).create();         ad.setIcon(android.R.drawable.ic_menu_camera);         ad.setTitle("Save Image");         ad.setMessage("Save This Image To Album?");         ad.setButton("Ok", this);          ad.show();*/            bmp = (Bitmap) extras.get("data"); // Set the bitmap to the bundle                                             // of data that was just                                             // received         image.setImageBitmap(bmp); // Set imageview to image that was                                     // captured         image.setScaleType(ScaleType.FIT_XY);       }  } 
like image 388
Jade Byfield Avatar asked Jan 25 '12 02:01

Jade Byfield


1 Answers

First lets make it clear - we have two options to take image data in onActivityResult from Camera:

1. Start Camera by passing the exact location Uri of image where you want to save.

2. Just Start Camera don't pass any Loaction Uri.


1 . IN FIRST CASE :

Start Camera by passing image Uri where you want to save:

String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/picture.jpg";   File imageFile = new File(imageFilePath);  Uri imageFileUri = Uri.fromFile(imageFile); // convert path to Uri  Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);  startActivityForResult(it, CAMERA_RESULT); 

In onActivityResult receive the image as:

@Override  protected void onActivityResult(int requestCode, int resultCode, Intent data) {      super.onActivityResult(requestCode, resultCode, data);       if (RESULT_OK == resultCode) {          iv = (ImageView) findViewById(R.id.ReturnedImageView);           // Decode it for real          BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();         bmpFactoryOptions.inJustDecodeBounds = false;           //imageFilePath image path which you pass with intent          Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);           // Display it          iv.setImageBitmap(bmp);      }     }  

2 . IN SECOND CASE:

Start Camera without passing image Uri, if you want to receive image in Intent(data) :

Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  startActivityForResult(it, CAMERA_RESULT);  

In onActivityResult recive image as:

@Override  protected void onActivityResult(int requestCode, int resultCode, Intent data) {      super.onActivityResult(requestCode, resultCode, data);       if (RESULT_OK == resultCode) {          // Get Extra from the intent          Bundle extras = data.getExtras();          // Get the returned image from extra          Bitmap bmp = (Bitmap) extras.get("data");           iv = (ImageView) findViewById(R.id.ReturnedImageView);          iv.setImageBitmap(bmp);      }  }  


*****Happy Coding!!!!*****
like image 139
ρяσѕρєя K Avatar answered Oct 14 '22 17:10

ρяσѕρєя K