ImageView displays the image rotated by 90 degrees

1

When I upload a photo to the server and I'm going to display it using

Picasso.with(getApplicationContext()).load(Caminhofoto).into(fotoalerta);

If the photo was taken with the mobile phone vertically, it is loaded horizontally and could display correctly. I have this same problem when loading it from SDCARD plus I am using this code to display properly.

 public String getRealPathFromURI(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    @SuppressWarnings("deprecation")
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}



public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

    try{
        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
            default:
                return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return bmRotated;
        }
        catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }
    catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

My UPLOAD class of the image

public class classe_FTP
{
    FTPClient mFtp;
    private String TAG = "classeFTP";

public boolean MudarDiretorio(String Diretorio)
{
    try
    {
        mFtp.changeWorkingDirectory(Diretorio);
    }
    catch(Exception e)
    {
        Log.e(TAG, "Erro: não foi possível mudar o diretório para " + Diretorio);
    }
    return false;
}

public boolean Desconectar()
{
    try
    {
        mFtp.disconnect();
        mFtp = null;
        return true;
    }
    catch (Exception e)
    {
        Log.e(TAG, "Erro: ao desconectar. " + e.getMessage());
    }

    return false;
}

public boolean Conectar(String Host, String Usuario, String Senha, int Porta)
{
    try
    {
        mFtp = new FTPClient();

        mFtp.connect(Host, Porta);

        if (FTPReply.isPositiveCompletion(mFtp.getReplyCode()))
        {
            boolean status = mFtp.login(Usuario, Senha);

            mFtp.setFileType(FTP.BINARY_FILE_TYPE);
            mFtp.enterLocalPassiveMode();

            return status;
        }
    }
    catch(Exception e)
    {
        Log.e(TAG, "Erro: não foi possível conectar" + Host);
    }
    return false;
}



public boolean Upload(String diretorio, String nomeArquivo)
{
    boolean status = false;
    try
    {
        FileInputStream arqEnviar = new FileInputStream(Environment.getExternalStorageDirectory() + diretorio);
        mFtp.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
        mFtp.setFileType(FTPClient.STREAM_TRANSFER_MODE);
        mFtp.storeFile("/patrulhar.com.br/imagens/"+nomeArquivo, arqEnviar);
        Desconectar();
        System.out.println("Upload win1 . ");
        return status;
    }
    catch (Exception e)
    {
        Log.e(TAG, "Erro: Falha ao efetuar Upload1. " + e.getMessage());
    }
    return status;
}

public boolean Upload2(String diretorio, String nomeArquivo)
{
    boolean status = false;
    try
    {
        FileInputStream arqEnviar = new FileInputStream(diretorio);
        mFtp.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
        mFtp.setFileType(FTPClient.STREAM_TRANSFER_MODE);
        mFtp.storeFile("/patrulhar.com.br/imagens/"+nomeArquivo, arqEnviar);
        Desconectar();
        System.out.println("Upload win2 . ");
        return status;
    }
    catch (Exception e)
    {
        Log.e(TAG, "Erro: Falha ao efetuar Upload2. " + e.getMessage());
    }
    return status;
}
}
    
asked by anonymous 17.03.2016 / 15:57

1 answer

0

Through the Picasso API you can access Bitmap after load using transform () do RequestCreator .

Create a class that implements the Transformation interface:

public class ImageTransformation implements Transformation {
    @Override public Bitmap transform(Bitmap source) {

        //Faça aqui o que quiser com o bitmap e retorne-o.
    }

    @Override public String key() { return "square()"; }
}

Use this way:

ImageTransformation imageTransformation = new ImageTransformation();
Picasso.with(getApplicationContext())
       .load(Caminhofoto)
       .transform(imageTransformation)
       .into(fotoalerta);

However, since you only want to rotate the image, RequestCreator provides the rotate () for this purpose:

Picasso.with(getApplicationContext())
       .load(Caminhofoto)
       .rotate(degrees)
       .into(fotoalerta);

or

Picasso.with(getApplicationContext())
       .load(Caminhofoto)
       .rotate(degrees, pivotX, pivotY)
       .into(fotoalerta); 
    
17.03.2016 / 20:41