Compress Android Studio File

Asked

Viewed 215 times

0

I’m trying to compress my File which in case is an image but it keeps going with sizes above 4MB someone would help me. Follows code below:

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_TAKE_PHOTO || requestCode == REQUEST_PICK_PHOTO) {
            if (data != null) {
                // Get the Image from data
                Uri selectedImage = data.getData();

                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                assert cursor != null;
                cursor.moveToFirst();


                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                mediaPath = cursor.getString(columnIndex);

                // REDUZIR O TAMANHO DO ARQUIVO

                Bitmap bmp = BitmapFactory.decodeFile(mediaPath);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.JPEG, 50, bos);


                if(imageView == null) {
                    imageView.setImageBitmap(BitmapFactory.decodeFile(mediaPath));
                }
                else if(imageView != null){
                    imageView2.setImageBitmap(BitmapFactory.decodeFile(mediaPath));
                } else if(imageView2 != null) {
                    imageView3.setImageBitmap(BitmapFactory.decodeFile(mediaPath));
                }
                cursor.close();

                postPath = mediaPath;
            }


        }else if (requestCode == CAMERA_PIC_REQUEST){
            if (Build.VERSION.SDK_INT > 21) {

                Glide.with(this).load(mImageFileLocation).into(imageView);
                postPath = mImageFileLocation;

            }else{
                Glide.with(this).load(fileUri).into(imageView);
                postPath = fileUri.getPath();
            }
        }
    }
    else if (resultCode != RESULT_CANCELED) {
        Toast.makeText(this, "Ocorreu um erro!", Toast.LENGTH_LONG).show();
    }
}

It’s probably something silly but I can’t seem to fix it, so please help me.

  • Images of type JPG, PNG are already compressed. These compression algorithms work well for text, or completely uncompressed images (.bmp, for example - uncompressed, an image can easily reach 200MB).

  • If you think 4MB is too big, you have to decrease (scale) the image

  • You’d have to give me a light and show me what the code would look like ?

  • I need it in KB to upload

  • The link I sent above has examples of code that will decrease the weight of the application at runtime. If it doesn’t suit the approach there, you will probably have to manually decrease the resolution (with photoshop, Irfanview, etc).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.