In android when taking vertical photo, in the image view, the image comes out rotated 90 degrees

2

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

    
asked by 12.06.2017 в 09:25
source

2 answers

1

I found the solution thanks to the comment of Angel Cid

What I have done is analyze the image once it arrives at the server, instead of modifying it from android. The PHP code

<?php
if (move_uploaded_file($_FILES['imagenNoticia']['tmp_name'], $rutaFichero))
{
    $archivo = explode(".", $rutaFichero);

    if ($archivo[1] == 'jpg' || $archivo[1] == 'jpeg')
    {
        $imagenTemporal = imagecreatefromjpeg($imagenOriginal);
    }
    else if($archivo[1] == 'JPG' || $archivo[1] == 'JPEG')
    {
        $imagenTemporal = ImageCreateFromJPEG($imagenOriginal);
    }
    else if($archivo[1] == 'png')
    {
        $imagenTemporal = imagecreatefrompng($imagenOriginal);
    }
    else
    {
        $imagenTemporal = imagecreatefromjpeg($imagenOriginal);
    }

    $exifAux = exif_read_data($_FILES['imagenNoticia']['tmp_name']);
    if(isset($exifAux['Orientation']))
    {
        switch($exifAux['Orientation'])
        {
            case 8:
                $imagenTemporal = imagerotate($imagenTemporal, 90, 0);
                break;
            case 3:
                $imagenTemporal = imagerotate($imagenTemporal, 180, 0);
                break;
            case 6:
                $imagenTemporal = imagerotate($imagenTemporal, -90, 0);
                break;
        }
    }
}
?>
    
answered by 13.06.2017 / 12:01
source
0

Unfortunately, this has a bit to do with the fragmentation of Android . I've had this same problem before, and on one device Samsung was rotated, while on another device it went well.

What I did was look at the height and width of the photo, and depending on that, decide whether it was vertical or horizontal.

Although you can also use ExifInterface

With it, you can do something like:

ExifInterface exif = new ExifInterface(uri.getPath());

and get the orientation with:

int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    
answered by 13.06.2017 в 12:08