Reduce Android Studio Image

0

I would like to reduce the quality of an image that I take from my phone in order to upload it to my Database and be able to download it without having to wait for a long time.

Method to take the image and compress it to upload it to the Database:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    int width=300;
    int heigth=240;
    if (resultCode == RESULT_OK) {

        switch (requestCode) {
            case COD_SELECCIONA:
                Bitmap originBitmap = null;
                Uri miPath = data.getData();
                image.setImageURI(miPath);

                convertphoto(miPath);


                break;

            case COD_FOTO:

                MediaScannerConnection.scanFile(getActivity(), new String[]{path}, null,
                        new MediaScannerConnection.OnScanCompletedListener() {
                            @Override
                            public void onScanCompleted(String path, Uri uri) {

                                Log.i("Ruta de almacenamiento", "Path: " + path);
                            }
                        });

                Bitmap bitmap = BitmapFactory.decodeFile(path);
                image.setImageBitmap(bitmap);
                Bitmap image = ((BitmapDrawable) this.image.getDrawable()).getBitmap();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                image.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
                byteArray = byteArrayOutputStream.toByteArray();
                encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);


            break;
        }


    }
}

Method to compress the photo taken from the gallery:

private void convertphoto(Uri selectedImage) {
    Bitmap originBitmap = null;
    //Uri selectedImage = data.getData();
   // Toast.makeText(getActivity(), selectedImage.toString(), Toast.LENGTH_LONG).show();
    InputStream imageStream;
    try {
        imageStream = getActivity().getContentResolver().openInputStream(selectedImage);
        originBitmap = BitmapFactory.decodeStream(imageStream);
    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage().toString());
    }
    if (originBitmap != null) {
        this.image.setImageBitmap(originBitmap);
        Log.w("Image Setted in", "Done Loading Image");
        try {
            Bitmap image = ((BitmapDrawable) this.image.getDrawable()).getBitmap();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();



            image.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
            byteArray = byteArrayOutputStream.toByteArray();


            encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
            // Calling the background process so that application wont slow down

            //End Calling the background process so that application wont slow down
        } catch (Exception e) {
            Log.w("OOooooooooo", "exception");
        }
       // Toast.makeText(getActivity(), "Conversion Done", Toast.LENGTH_SHORT).show();
    }
    // End getting the selected image, setting in imageview and converting it to byte and base 64
}
    
asked by Julio Mizrahi 18.09.2018 в 13:52
source

1 answer

0

Instead of using the compress method, try using the Bitmap.createScaledBitmap function. It would be to call the function after obtaining the bitmap:

Bitmap image = ((BitmapDrawable) this.image.getDrawable()).getBitmap();
Bitmap imageScaled = Bitmap.createScaledBitmap(image, newWidth, newHeight, false);
    
answered by 19.09.2018 / 08:01
source