0

I'm currently making an android photography app. In my upload fragment i have a camera intent and a gallery intent, however when i display the image to the image view, the quality of the image lowers. here is my code:

    public void CameraIntent(){
    cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    file = new File(Environment.getExternalStorageDirectory(),
            "file"+String.valueOf(System.currentTimeMillis())+".jpg");
    uri = Uri.fromFile(file);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT , uri);
    cameraIntent.putExtra("return-data",true);
    startActivityForResult(cameraIntent,0);
}
public void GalleryIntent(){
    galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(Intent.createChooser(galleryIntent,"Select Image from Gallery"),2);
}
    public void cropImage(){
    try{
        CropIntent = new Intent("com.android.camera.action.CROP");
        CropIntent.setDataAndType(uri,"image/*");

        CropIntent.putExtra("crop","true");
        CropIntent.putExtra("outputX",180);
        CropIntent.putExtra("outputY",180);
        CropIntent.putExtra("aspectX",3);
        CropIntent.putExtra("aspectY",3);
        CropIntent.putExtra("scaleUpIfNeeded",true);
        CropIntent.putExtra("return-data",true);

        startActivityForResult(CropIntent,1);
    }
    catch (ActivityNotFoundException ex)
    {
        ex.printStackTrace();
    }
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == 0 && resultCode == RESULT_OK) {
        cropImage();
    }
    else if(requestCode == 2)
    {
        if(data != null)
        {
            uri = data.getData();
            cropImage();
        }
    }
    else if (requestCode == 1 && resultCode == RESULT_OK)
    {
        if(data != null)
        {
            bundle = data.getExtras();
            UriF = data.getData();

            bitmap = (Bitmap)data.getExtras().get("data");
            imageView.setImageBitmap(bitmap);
            seePreview.setEnabled(true);
            submitUpload.setEnabled(true);
            seePreview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    titleS = title.getText().toString();
                    descriptionS = description.getText().toString();
                    openPreview(titleS , descriptionS , bitmap, UriF);
                }
            });
            submitUpload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    submit(bitmap);
                }
            });
        }
    }

I observed a significant loss of quality. here is the actual photo: Actual photo

Here is what it looked like when i put it in the imageview Photo after putting to the image view

there is a significant change in the quality. i tried using the URI, however, UriF = data.getdata() returns null.

  • `else if (requestCode == 1 && resultCode == RESULT_OK)` There is no requestcode with value 1 in your post. Moreover if you talk about two intents you should tell also with with intent the quality is degraded. And no need to post code that does what you want. – greenapps Mar 01 '18 at 17:18
  • the request code 1 is found in the crop image. so making many intents lessens the quality of the picture? what do you suggest i do to make the quality better? lessen the intents? – alfonso claros Mar 01 '18 at 17:26
  • You talk nonsense. You did not answer my questios. – greenapps Mar 01 '18 at 18:51
  • `outputX` and `outputY` are in `px`. `180px` is very less. Can you try with `500px` – Vishu Mar 01 '18 at 19:30
  • I marked the 'duplicate' for the first historical appearance of your question, but the answers there may be too terse. Check [this answer](https://stackoverflow.com/a/10382217/192373) for more details. **`TL;NR`**: You should not use `(Bitmap)data.getExtras().get("data")` because it is just a thumbnail, too small for your purpose. You should not look for image file URI in the intent returned to `onActivityResult()` when you provide `MediaStore.EXTRA_OUTPUT`. You should pass this URI to `onActivityResult()` within your app, and [use a FIleProvider](https://stackoverflow.com/a/18332000/192373). – Alex Cohn Mar 01 '18 at 23:26

0 Answers0