Working with camera and gallery

Asked

Viewed 816 times

2

I am with the following problem, in my application I take a photo and present her in a imageView, but when I take the photo she gets the good quality in the gallery imageView no, if it is a text not even to read, lower I will put the camera code

private void cameraIntent() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, REQUEST_CAMERA);
}

private void onCaptureImageResult(Intent data) {
    bitmap = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
    FileOutputStream fo;

    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    imageView.setImageBitmap(bitmap);

    fab.setVisibility(View.VISIBLE);
}

I imagine changing the way you search for the photo solves the problem.

In this same application also has the option to select an image from the gallery and in this option the image comes perfect without change in quality.

 private void galleryIntent() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
}

@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
    if (data == null)
        return;
    imagemUrl = data.getDataString();
    if (imagemUrl != null) {
        Glide
                .with(this)
                .load(imagemUrl)
                .into(imageView);
    }
    fab.setVisibility(View.VISIBLE);

}

1 answer

1

The problem is simple, but it is also a common source of confusion of who is starting to work with image capture with Intent on android.

The 'X' of the question is here: bitmap = (Bitmap) data.getExtras().get("data");

This Extras field, returned by the camera intention, does not keep the captured image, but a small thumbnail (thumbnail) of the image.

The correct thing, when working with the camera on android, is to inform the intention of camera the target file that you want to be generated with the captured image (in full resolution). For this use the code below:

Uri outputUri;

private void cameraIntent() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // outputUri precisa apontar para um arquivo que seu app tenha direito de gravar
    outputUri = getTempCameraUri();
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
    startActivityForResult(intent, REQUEST_CAMERA);
}

private Uri getTempCameraUri() {
    try {
        // uso createTempFile por conveniência
        File file = File.createTempFile("camera", ".jpg", this.getExternalCacheDir());
        // se estiver em um Fragment, use getActivity() ao invés de this na linha anterior
        return Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

After the execution of the camera intention, when the user returns, the photo taken by the user will be recorded in outputUri. Now just modify your code to:

private void onCaptureImageResult(Intent data) {
    if (outputUri == null)
        return;

    Glide
        .with(this)
        .load(outputUri)
        .centerCrop() // não deixe de aplicar alguma transformação!!!
        .into(imageView);
    }
    fab.setVisibility(View.VISIBLE);
}

And voilà!

  • did not work, by the test I did it is not recording anything inside the variable outputUri, for that reason it is not loading anything in the imageview, but the image is being recorded in the gallery

  • @Vitorhugo You have to assign a value to outputUri as I said, otherwise it will be passed as null to the camera. It’s not the camera that decides where to record and fills outputUri with a value, it USES the value you passed in outputUri to record in that location that YOU reported. I usually get a temporary file before. You filled out outputUri and the camera didn’t record anything there? Was it? Or didn’t fill in a value?

  • i imagined that this line ( Intent.putExtra(Mediastore.EXTRA_OUTPUT, outputUri); ) recorded a value inside the outputUri, how do I get the Uri of the image that was generated ?

  • @Vitorhugo this line only puts the content of the outputUri in the extra of the Intent that goes to the camera. Soon the outputUri already has to be with a path defined by you to where the image will be recorded. It is a way to pass a pro Intent parameter indicating the path of the image you want saved. Tomorrow morning I’ll edit the answer to make it clearer.

  • the only way I know to get the Uri value and through the data Intent that is not working out, I will wait, thanks again, you are the one who is helping me, I imagined that would have more android programmers

  • @Vitorhugo is bm simple to solve this! You just need to build the given Uri a path. I will update the code with a routine that creates a temporary file which you can then pass to the insertDb the URI that will be copied, with the ones selected by the gallery... In the future you can even already make the camera record straight in this directory, or if you prefer to play in the local directory later, hare up delete the temporary that create, not to leave a lot of photos in the temporary and in the directory pointed by the BD.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.