Save Image with better resolution

2

Hello, I'm saving images to the database. But the resolution is very bad. I'm using the following:

btncamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tirarfoto();

        }
    });

    btngaleria.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galeriaIntent = new Intent(Intent.ACTION_GET_CONTENT);
            galeriaIntent.setType("image/*");

            startActivityForResult(galeriaIntent, ESCOLHERFOTO);


        }
    });

In another section I use this:

 if (foto != null) {
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    foto.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    byte[] bytearray = stream.toByteArray();

                    final ParseFile fileimagem = new ParseFile("imagem.jpg", bytearray);
                    fileimagem.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {

                            if (e == null) {
                                salvarDica(fileimagem);


                            }
                        }
                    });
                } else {
                    salvarDica(null);
                }

And I also have these methods:

private void tirarfoto() {
    Intent abrircamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    if (abrircamera.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(abrircamera, TIRARFOTO);
    }
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ESCOLHERFOTO && resultCode == RESULT_OK) {

        uricaminhodafoto = data.getData();
        try {
            foto = MediaStore.Images.Media.getBitmap(getContentResolver(), uricaminhodafoto);
            foto = Bitmap.createScaledBitmap(foto, 200, 200, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        picture.setImageBitmap(foto);


    }


    if (requestCode == TIRARFOTO && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        foto = (Bitmap) extras.get("data");
        picture.setImageBitmap(foto);


    }
}

Part of my setup:

 <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/picture"
            android:src="@drawable/picture"
            android:layout_gravity="center_horizontal"
            android:scaleType="centerCrop"
            android:adjustViewBounds="true"
            android:cropToPadding="false" />

The resolution of the saved image is terrible. Does anyone know how to solve it? Thank you.

    
asked by anonymous 29.11.2016 / 05:31

2 answers

2

Try replacing this:

foto = Bitmap.createScaledBitmap(foto, 200, 200, false);

So:

foto = Bitmap.createScaledBitmap(foto, 1000, 1000, false);

Another alternative is to just remove this line and stop doing resize of Bitmap .

    
29.11.2016 / 10:07
0

Well, guys, thank you very much. I've put them together and found a good solution for me. I could not get the resolution of the photo taken from the camera look good, so I just let it come from the gallery. But for my App, that's enough. Now what makes the photo decrease greatly in size (to be in the exact size of my ImageView) without losing the definition and being very light to be uploaded and for the user to save in the bank was this:

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ESCOLHERFOTO && resultCode == RESULT_OK) {

        uricaminhodafoto = data.getData();
        try {
            foto = MediaStore.Images.Media.getBitmap(getContentResolver(), uricaminhodafoto);


            foto2 = getResizedBitmap(foto);
        } catch (IOException e) {
            e.printStackTrace();
        }

        picture.setImageBitmap(foto2);


    }



}

public Bitmap getResizedBitmap(Bitmap foto) {
    int width = foto.getWidth();
    int height = foto.getHeight();
    if (height > width) {


        newHeight = 400;
        newWidth = (width * 400) / height;
    }
    if (height == width) {

        newHeight = 240;
        newWidth = 240;
    }


    if (width > height) {

        newWidth = 760;
        newHeight = (height * 760 / width);
    }

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(
            foto, 0, 0, width, height, matrix, false);
    foto.recycle();

    return resizedBitmap;
}
    
04.12.2016 / 19:40