Newtt
Newtt

Reputation: 6200

Android capture and use image not working

I've written code in Android to capture an image and then use that image as follows:

private static Intent i;
final static int cons = 0;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public void takePicture()
{
    i= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //Open camera
    startActivityForResult(i, cons);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Log.d("Testing if condition", "Test"); //This code does not execute
            Bundle ext = data.getExtras();
            Log.d("Upload image", ext.toString());
            sendForUpload(data); // function to upload file to server
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }
}

This code lets me take a photo and it saves to the SD card. However, I'm not sure if the file is sent to the sendForUpload function where I've handled getting the path and uploading the file. In fact nothing inside the if (resultCode == RESULT_OK) block works. How do I use the image captured from this activity?

Upvotes: 0

Views: 416

Answers (2)

Ika8
Ika8

Reputation: 391

I'm using this to put the image in a ImageView:

Uri uri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

You can use bitmap or uri objects for upload or show your picture..

Hope it helps you.

Upvotes: -1

CommonsWare
CommonsWare

Reputation: 1007658

Well, you have a few problems.

First, you are calling startActivityForResult(i, cons);. cons is 0. You are trying to compare 0 with CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE. Since CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE is 100, that will not work.

Second, you are calling data.getExtras().toString() (split over two Java statements). I would expect this to return a value like android.os.Bundle@34334143. If that is what you want to upload, fine. I am guessing that you want to upload the image. Since you did not include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, you get a Bitmap of the thumbnail image taken by the camera via data.getParcelableExtra("data").

Third, you are making inappropriate assumptions:

This code lets me take a photo and it saves to the SD card.

There is no requirement for the camera app to save the image anywhere, let alone to removable storage. In fact, I would argue that this is a bug in your camera app. That is not surprising, as ACTION_IMAGE_CAPTURE implementations have bugs.

where I've handled getting the path and uploading the file

There is no path. You did not include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, and so all you get back is the thumbnail Bitmap.

Upvotes: 2

Related Questions