Android - Get URI from image I just took (without saving the image again)

I've found many answers to questions like mine, but it doesn't make sense to me. Let me explain.

I have an ImageButton that allows the user to take a snapshot and display it on the interface. When I try to get the URI of the image, it returns null:

Uri uri = data.getData(); 

      

I did some searches on the internet and found solutions like:

@Override
public void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    try {
        if (resultCode == Activity.RESULT_OK) {
            updateProfilePicure = Boolean.TRUE;
            switch(requestCode){
                case 0:
                    Bundle extras = data.getExtras();
                    Object xx = data.getData();
                    Bitmap imageBitmap = (Bitmap) extras.get("data");
                    Uri tempUri = getImageUri(imageBitmap);
                    imageView.setImageBitmap(imageBitmap);
                    break;
                default: break;
            }
        }
    } catch(Exception e){
        e.printStackTrace();
    }
}

public Uri getImageUri(Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(
            ApplicationContext.getInstance().getContext().getContentResolver(), inImage,
   "Title", null);
    return Uri.parse(path);
}   

      

It doesn't make sense to me because when the call goes to the onActivityResult () method the image is already saved in the DCIM folder and has no reason to save it. So why should I use it?

Can I find another way to get the URI from the captured image?

Thanks in advance.

+3


source to share


2 answers


the picture has already been saved in the DCIM folder and has no reason to save it.

Not necessary. Quoting the documentation forACTION_IMAGE_CAPTURE

:

The caller can pass an additional EXTRA_OUTPUT to control where this image will be written. If EXTRA_OUTPUT is not present, the small image is returned as a Bitmap in an optional field.

(extra field added here) 21>)



The code snippet you injected fetches data

extra and therefore the image is not saved anywhere.

Can I find another way to get the URI from the captured image?

You already have the code for this, in your first code snippet - if you specify Uri

as EXTRA_OUTPUT

in your request ACTION_IMAGE_CAPTURE

, you will get Uri

back to the image that was taken in Intent

, passed in onActivityResult()

.

+5


source


Take a look at this link: http://developer.android.com/training/camera/photobasics.html

I worked a lot with images in my last project. When creating your image, use something like this:

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) 
{
    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}

      

The image is not saved (at least it is not in my project). You can get the sketch directly using this code:



Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");

      

If you want to have a full-sized image, you must save it:

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
    // Create the File where the photo should go
    File photoFile = null;
    try {
        // This is where the file is created, create it as you wish. For more information about this, see the link or add a comment
        photoFile = createImageFile();
    } catch (IOException ex) {
        // Error occurred while creating the File
        ...
    }
    // Continue only if the File was successfully created
    if (photoFile != null) {
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(photoFile));
        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
    }
}

      

+2


source







All Articles