2

I'm trying to set image taken from the camera into an ImageView. I launch the camera intent and then I got a NullPointerException in OnActivityResult an I don't understand the error.

Here, I launche the camera intent and I store the image in the gallery of the phone :

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
imageUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_REQUEST);

Now, the image token by the camera is saved in the gallery of the phone (high quality). In onActivityResult, I want to put the image into a ImageView. This is my code :

else if (requestCode == CAMERA_REQUEST)
{
  if (resultCode == RESULT_OK)
   {
     imageUri = data.getData();
             try 
             {
               Bitmap bitmap = Media.getBitmap(getContentResolver(), imageUri); //NullPointerException
               myImageView.setImageBitmap(bitmap);
             } 
          catch (IOException e) 
             {
              e.printStackTrace();
             }

I have a NullPointerException, why ? How can I resolve it please ?

2 Answers2

1

This make your result save in mediaStore, so data.getData(); will return NULL:

pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

Remove it and you will get data for show on ImageView. However, if you get image from data directly, it's in bad quality

eleven
  • 41
  • 1
  • 8
  • Exactly, I fact I want to send this image via mail in high quality. So I need the ImageUri. Can I do this please ? –  Jan 20 '14 at 17:12
  • Use imageUri directly instead of use `data.getData();`. You can get path from imageUri and use `BitmapFactory.decodeFile(path)` to create bitmap – eleven Jan 20 '14 at 17:32
  • Change parentActionIntent to intent. And change second part to this: `if (resultCode == RESULT_OK) { try { Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath()); myImageView.setImageBitmap(bitmap); } catch (IOException e) { e.printStackTrace(); }` I dont know completely your code,so i'm not sure it's work. if inneed, i can post a sample code for this purpose – eleven Jan 20 '14 at 18:17
  • Thank you for you response. But it didn't work for me –  Jan 21 '14 at 08:52
0

Your code is incomplete and you haven't added the logcat errors! so in this condition it very difficult to guide you! but what you want to acheive is not that much difficult.Try this:

public class LaunchCamera extends Activity {
 ImageView imVCature_pic;
 Button btnCapture;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_launch_camera);
  initializeControls();
 }

 private void initializeControls() {
  imVCature_pic=(ImageView)findViewById(R.id.imVCature_pic);
  btnCapture=(Button)findViewById(R.id.btnCapture);
  btnCapture.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    /* create an instance of intent
     * pass action android.media.action.IMAGE_CAPTURE 
     * as argument to launch camera
     */
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    /*create instance of File with name img.jpg*/
    File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
    /*put uri as extra in intent object*/
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    /*start activity for result pass intent as argument and request code */
    startActivityForResult(intent, 1);
   }
  });

 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  //if request code is same we pass as argument in startActivityForResult
  if(requestCode==1){
   //create instance of File with same name we created before to get image from storage
   File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
   //get bitmap from path with size of
   imVCature_pic.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 600, 450));
   }
 }
 public static Bitmap decodeSampledBitmapFromFile(String path,
   int reqWidth, int reqHeight) { 
  // First decode with inJustDecodeBounds=true to check dimensions
  final BitmapFactory.Options options = new BitmapFactory.Options();
  //Query bitmap without allocating memory
  options.inJustDecodeBounds = true;
  //decode file from path
  BitmapFactory.decodeFile(path, options);
  // Calculate inSampleSize
  // Raw height and width of image
  final int height = options.outHeight;
  final int width = options.outWidth;
  //decode according to configuration or according best match
  options.inPreferredConfig = Bitmap.Config.RGB_565;
  int inSampleSize = 1;
  if (height > reqHeight) {
   inSampleSize = Math.round((float)height / (float)reqHeight);
  }
  int expectedWidth = width / inSampleSize;
  if (expectedWidth > reqWidth) {
   //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
   inSampleSize = Math.round((float)width / (float)reqWidth);
  }
  //if value is greater than 1,sub sample the original image
  options.inSampleSize = inSampleSize;
  // Decode bitmap with inSampleSize set
  options.inJustDecodeBounds = false;
  return BitmapFactory.decodeFile(path, options);
 }
}

complete code snippet is described well here.

Hamad
  • 5,096
  • 13
  • 37
  • 65
  • Thank you very much for the link. However, I need the imageUri to send the image token from camera by email. How can I do this please ? –  Jan 20 '14 at 23:04
  • do upvote the answers! for others help! and ok this link will help you! in email sending. http://stackoverflow.com/questions/1247983/problem-sending-an-email-with-an-attachment-programmatically – Hamad Jan 21 '14 at 05:36