Why doesn't it create the .png I'm trying to get it to create from the selected image?
public class ClaseNueva extends Activity {
private ImageView imagenPersona;
private int SELECCIONAR_IMAGEN = 237487;
private Uri pathImagenUri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clase_nueva);
imagenPersona = (ImageView) findViewById(R.id.imagenPersona);
imagenPersona.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ventanaImagen();
}
});
}
private void ventanaImagen() {
try {
final CharSequence[] items = {"Seleccionar de la galería"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Seleccionar una foto");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0:
Intent intentSeleccionarImagen = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
intentSeleccionarImagen.setType("image/*");
startActivityForResult(intentSeleccionarImagen, SELECCIONAR_IMAGEN);
break;
}
}
});
AlertDialog alert = builder.create();
alert.show();
} catch (Exception e) {
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECCIONAR_IMAGEN && resultCode == Activity.RESULT_OK) {
if (pathImagenUri != null) {
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File file = new File(dir, "resized_image.png");
Uri imagenSeleccionada = pathImagenUri;
Bitmap bitmap = getBitmap(pathImagenUri.getPath());
if (bitmap != null) { //Bitmap no es null?
//Redimnsiona imagen.
Bitmap bitmapout = Bitmap.createScaledBitmap(bitmap, 150, 150, false);
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmapout.compress(Bitmap.CompressFormat.PNG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "Error obteniendo imagen", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "Error obteniendo imagen", Toast.LENGTH_LONG).show();
}
}
}
private Bitmap getBitmap(String ruta_imagen) {
File imagenArchivo = new File(ruta_imagen);
Bitmap bitmap = null;
if (imagenArchivo.exists()) {
bitmap = BitmapFactory.decodeFile(imagenArchivo.getAbsolutePath());
}
return bitmap;
}
}
I don't get any errors, it just doesn't get created, why is that?
If you use
startActivityForResult()
when receiving the image data through the bundle inonActivityResult()
, you can get the path in this way create the bitmap, resize it and save it in another path (variablenuevaRutaArchivo
);At the end the image with new measure is added to the ImageView:
There is the function:
The last parameter is the algorithm that is followed to do the resizing.
This using the swing package .
JavaDoc for more information.