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).
– Leonardo Alves Machado
If you think 4MB is too big, you have to decrease (scale) the image
– Leonardo Alves Machado
You’d have to give me a light and show me what the code would look like ?
– Ronaldo Amaral
I need it in KB to upload
– Ronaldo Amaral
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).
– Leonardo Alves Machado