I have an application in development where I upload an image to an external server.
My problem is: if the user has a cell phone with a good resolution camera, the images have a large size ... and they are not shown in the ImageView, nor are they uploaded to the server because of the weight in MB that they have .
I have researched some examples and I have found that something like the following is used:
To rescale the image:
Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);
To compress the image:
Bitmap bmp = BitmapFactory.decodeFile(miFoto)
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 70, bos);
InputStream in = new ByteArrayInputStream(bos.toByteArray());
ContentBody foto = new InputStreamBody(in, "image/jpeg", "filename");
But I do not know how to use this code to adapt it to my code. Or where part of my code goes.
This is my code to get the images:
private void showOptions(){
final CharSequence[] option = {"Tomar foto", "Elegir de galeria", "Cancelar"};
final AlertDialog.Builder builder = new AlertDialog.Builder(Modificar.this);
builder.setTitle("Elige una opción");
builder.setItems(option, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(option[which] == "Tomar foto"){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
//Verifica permisos para Android 6.0+
checkExternalStoragePermission();
}
File file = new File(Environment.getExternalStorageDirectory(), MEDIA_DIRECTORY);
boolean isDirectoryCreated = file.exists();
if(!isDirectoryCreated)
isDirectoryCreated = file.mkdirs();
if(isDirectoryCreated){
Long timestamp = System.currentTimeMillis() / 1000;
imageName = timestamp.toString() + ".jpg";
mPath = Environment.getExternalStorageDirectory() + File.separator + MEDIA_DIRECTORY
+ File.separator + imageName;
newFile = new File(mPath);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(newFile));
startActivityForResult(intent, PHOTO_CODE);
}
} else if(option[which] == "Elegir de galeria"){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "Selecciones un administrador de archivos"),SELECT_PICTURE);
}catch (android.content.ActivityNotFoundException ex){
}
}else {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case SELECT_PICTURE:
if (resultCode == RESULT_OK){
subir.setEnabled(true);
Bitmap photobmp;
Uri selectedImageUri = data.getData();
String dataFU = getRealPathFromURI(selectedImageUri);
newFile = new File(dataFU);
photobmp = BitmapFactory.decodeFile(dataFU);
imagen.setImageBitmap(photobmp);
imageName = dataFU.substring(dataFU.lastIndexOf("/")+1);
nombreImagen.setText(imageName);
break;
}
case PHOTO_CODE:
if(resultCode == RESULT_OK) {
MediaScannerConnection.scanFile(this,
new String[]{mPath}, null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> Uri = " + uri);
}
});
Bitmap bitmap = BitmapFactory.decodeFile(mPath);
imagen.setImageBitmap(bitmap);
nombreImagen.setText(imageName);
subir.setEnabled(true);
break;
}
}
}
I hope you can support me!