Working with camera and gallery

2

I have the following problem, in my application I take a photo and present it in a imageView , but when I take the photo it gets the good quality in the gallery only in imageView no, if it is a text not even to read, lower I'll 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 look for the photo will solve the problem.

In this same application you also have the option to select an image from the gallery and in this option the image comes perfect without any 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);

}
    
asked by anonymous 21.10.2016 / 02:41

1 answer

1

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

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

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

The correct thing to do when working with the camera on android is to tell the camera intent the target file that you want to be generated with the captured image (at full resolution). To do 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 executing the camera intent, when the user returns, the picture taken by the user will be recorded in outputUri . Now just change 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à!

    
21.10.2016 / 03:56