Error photos with poor Android quality

0

I am trying to take a photo from my phone and display it in an ImageView. I follow the steps of the web of Android Studio in which it indicates how to take a photo and show it but when showing it in the ImageView it comes out with very little quality. I do not understand where I can be failing

I leave the code:

    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cameraIntent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);

            if (cameraIntent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        }
    });
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode,resultCode,data);
    if(requestCode == CAMERA_REQUEST){
        if(resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            imageBitmap = (Bitmap) extras.get("data");
            imageView.setImageBitmap(imageBitmap); // photo with low quality                
        }
    }
}

And the variables and constants;

private Intent cameraIntent;

private static final int CAMERA_REQUEST = 1888;
private Bitmap imageBitmap;
ImageView imageView;
    
asked by CMorillo 26.03.2018 в 19:42
source

1 answer

1

What happens is that the intent brings you the thumbnail . This in order to use that image to display in your interface as an icon or previous. If you want the camera to save your image and have access to the full quality image, you should send a File to your camera, and that's where the image will be saved in full size.

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
        // Crea el File
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error cuando se creo el archivo
        }
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(getContext().getApplicationContext(),"com.tuapp.example" ,
                        photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }

And in the createImageFile method, create the File that is sent in Intent and save the path of the whole image.

private File createImageFile() throws IOException {
        // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );  

    mCurrentPhotoPath = image.getAbsolutePath();
    Log.d(TAG,"el path de la imagen es = " + mCurrentPhotoPath);
    return image;
}

Finally, in your manifest it is necessary that you add a provider

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.tuapp.example"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>

After all those laps, in your onActivityResult , after validating that you try correctly, you can use the image creating a File with the full path of the file.

File file = new File(mCurrentPhotoPath);
if(file.exists()){
    //tu archivo existe, haz lo que necesites

In the documentation there is the Save the full-size photo section. Part of the code and an explanation of how to save the image can be found there.

    
answered by 26.03.2018 в 21:27