I have problems uploading an image to a server from android.
My code to select image:
new AlertDialog.Builder(getContext())
.setTitle("Seleccionar Imagen")
.setPositiveButton("Galeria", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
tipoImagen = true;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Selecciona Imagen"), PICK_IMAGE_REQUEST_GALLERY);//PICK_IMAGE_REQUEST_GALLERY=1
}
})
.setNegativeButton("Cámara", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
tipoImagen = false;
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, PICK_IMAGE_REQUEST_CAMERA);//PICK_IMAGE_REQUEST_CAMERA=5
}
})
.setIcon(android.R.drawable.ic_dialog_dialer)
.show();
My code to pick up the image:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST_CAMERA && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
uriPath = filePath;
String realPath;
// SDK < API11
if (Build.VERSION.SDK_INT < 19)// SDK >= 11 && SDK < 19
{
realPath = RealPathUtil.getRealPathFromURI_API11to18(getActivity(), data.getData());
}
else// SDK > 19 (Android 4.4)
{
realPath = RealPathUtil.getRealPathFromURI_API19(getActivity(), data.getData());
}
globalPath = realPath;
configImage(Build.VERSION.SDK_INT, data.getData().getPath(),realPath);
}
else if (requestCode == PICK_IMAGE_REQUEST_GALLERY && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
uriPath = filePath;
String realPath;
// SDK < API11
if (Build.VERSION.SDK_INT < 19)// SDK >= 11 && SDK < 19
{
realPath = RealPathUtil.getRealPathFromURI_API11to18(getActivity(), data.getData());
}
else// SDK >= 19 (Android 4.4)
{
realPath = RealPathUtil.getRealPathFromURI_API19(getActivity(), data.getData());
}
globalPath = realPath;
configImage(Build.VERSION.SDK_INT, data.getData().getPath(),realPath);
}
}
private void configImage(int sdk, String uriPath, String realPath){
Uri uriFromPath = Uri.fromFile(new File(realPath));
try {
bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uriFromPath));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
imagenFondo.setImageBitmap(bitmap);
imagenFondo.setVisibility(View.VISIBLE);
txtCamera.setVisibility(View.GONE);
Log.d("HMKCODE", "Build.VERSION.SDK_INT:" + sdk);
Log.d("HMKCODE", "URI Path:" + uriPath);
Log.d("HMKCODE", "Real Path: " + realPath);
}
The problem is that if I take an image in Vertical when it arrives at the server, the image appears rotated by default. How can I correct that automatic rotation?
Not only rotates the image when sent to the server, if I show it in an imageview it is also rotated