0

I am trying to give the user the option to either choose an image from the phone's storage or take one with the phone's camera app and then display that image in an imageView. With the help of this and this question I am able to display the image when it is choosen from the storage. However if I choose to take a picture with camera although I don't get an error the image is not shown in the imageView.

The function for choosing the picture looks like:

public void choosePic(View view) {
    Intent pickIntent = new Intent();
    pickIntent.setType("image/*");
    pickIntent.setAction(Intent.ACTION_GET_CONTENT);

    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
    Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
    chooserIntent.putExtra
    (
      Intent.EXTRA_INITIAL_INTENTS, 
      new Intent[] { takePhotoIntent }
    );

    startActivityForResult(chooserIntent, SELECT_PICTURE);
}

After that it is handled by

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == PICK_IMAGE && data != null && data.getData() != null) {
        Uri _uri = data.getData();

        //User had pick an image.
        Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
        cursor.moveToFirst();

        image = (ImageView) findViewById(R.id.imageView);
        image.setImageURI(_uri);
        cursor.close();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

EDIT

With @Murtaza Hussain's helps I was able to find a working solution:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i("requestCode",""+requestCode);
    if(requestCode == PICK_IMAGE && resultCode==RESULT_OK && data != null && data.getData() != null) {
        Uri _uri = data.getData();

        //User had pick an image.
        Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
        cursor.moveToFirst();

        image = (ImageView) findViewById(R.id.imageView);
        image.setImageURI(_uri);
        cursor.close();
    }
    else if(requestCode== PICK_IMAGE && resultCode==RESULT_OK && data.getData() == null){

        Bundle extras = data.getExtras();
        Bitmap thePic = extras.getParcelable("data");


        image =(ImageView) findViewById(R.id.imageView);
        image.setImageBitmap(thePic);

    }

    super.onActivityResult(requestCode, resultCode, data);
}
Community
  • 1
  • 1
Axel
  • 1,415
  • 1
  • 16
  • 40

2 Answers2

0

You need to add code for it in onActivityResult()

if(requestCode==0 && resultCode==RESULT_OK ){

    Bundle extras = data.getExtras();
    Bitmap thePic = extras.getParcelable("data");


    image =(ImageView) findViewById(R.id.imageView);
    image.setImageBitmap(thePic);

}
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
  • Are your sure, that requestCode==0 && resultCode==RESULT_OK is correct? Looks like this will never be true. – Axel Jan 14 '15 at 10:13
  • Sorry my bad. Thought it said two times requestCode, I think I need a coffee. Will add the updated code to my question. – Axel Jan 14 '15 at 10:15
  • try to log the request code. it should be different from gallery intent. – Murtaza Khursheed Hussain Jan 14 '15 at 10:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68808/discussion-between-axel-and-murtaza-hussain). – Axel Jan 14 '15 at 11:33
0

In your activity when to capture image from camera. write the below code.

Intent intent    = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"/imageLarge"+System.currentTimeMillis()+".jpg"));
                intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

                try {
                    intent.putExtra("return-data", true);
                    startActivityForResult(intent, 111);
                } catch (ActivityNotFoundException e) {
                    e.printStackTrace();
                }

In onActivityResult() write the below code..

   @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            switch (requestCode) {
            case 111:           

                if(resultCode == RESULT_OK)
                {
                    Log.v("","After camera capture path is --> "+mImageCaptureUri.toString().substring(8));         
                    Bitmap bitmap=BitmapFactory.decodeFile(mImageCaptureUri.toString().substring(8));
image.setImageBitmap(bitmap);

                }
                else
                {

                }
    }
    }

Do not forget to add one permission.

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

In case of any query, Please let me know. Thanks.

droidd
  • 1,361
  • 10
  • 15