使用Picasso从uri地址旋转图像

时间:2018-03-14 16:04:19

标签: android firebase rotation picasso

我已成功将用户个人资料图片上传到其UserID下的Firebase存储区域。

从那里,我已将文件路径放在用户ID

下的Firebase数据库中

Firebase Database Structure, Showing he profileImage uri that is linked to the firebase storage area

当我在页面上显示图像时,图像以错误的方式旋转。我手机上的照片是纵向照片,但它在Firebase存储区域中保存为横向照片。

Image Stored In Landscape orientation in Firebase Storage, but was taken in portrait orientation

The image rendered on my phone in the wrong orientation

我想要做的是让用户从图库中选择一个图像,然后在页面上显示图像。然后我希望能够让用户自己左右旋转图像。纽扣。

按下旋转按钮时。图像成功旋转一次。 然后,当我按下保存配置文件图像按钮时,它会将原始图像从库中发送到Firebase存储区域。它正在存储错误的图像并将其发送到存储。基本上,它将原始的,未旋转的图像保存到存储器中。

我有办法解决这个问题吗?

这是我的代码:

private FirebaseAuth auth;
private DatabaseReference myRef;
private FirebaseDatabase database;
private StorageReference storageReference;
private FirebaseUser user;

private ImageView profileImage;
private Uri imageUri;

private static final int GALLERY_INTENT = 2;

private ProgressDialog progressDialog;

/*
Skip irrelevant code
*/

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        View view = inflater.inflate(R.layout.fragment_upload_image, container, false);

        // Creating firebase links etc
        database = FirebaseDatabase.getInstance();
        myRef = FirebaseDatabase.getInstance().getReference();
        auth = FirebaseAuth.getInstance();
        storageReference = FirebaseStorage.getInstance().getReference();
        user = auth.getCurrentUser();

        // Setting buttons
        profileImage = (ImageView) view.findViewById(R.id.imageViewProfileImage);
        ImageView rotateLeft = (ImageView) view.findViewById(R.id.imageRotateLeft);
        ImageView rotateRight = (ImageView) view.findViewById(R.id.imageRotateRight);
        Button uploadProfileImage = (Button) view.findViewById(R.id.buttonUploadProfileImage);
        Button saveProfileImage = (Button) view.findViewById(R.id.buttonSaveProfileImage);

        // Rotate Left is a button
        rotateLeft.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(profileImage != null)
                {
                    // Rotate image 90
                    Picasso.get().load(imageUri).rotate(90).into(profileImage);
                }
            }
        });

        // Rotate Right is a button
        rotateRight.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(profileImage != null)
                {
                    // Rotate image -90
                    Picasso.get().load(imageUri).rotate(-90).into(profileImage);
                }
            }
        });

        uploadProfileImage.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // Send user to gallery
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, GALLERY_INTENT);
            }
        });

        // Save image to storage area
        saveProfileImage.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                progressDialog.setMessage("Uploading Image Please Wait...");
                progressDialog.show();

                final StorageReference filePath = storageReference.child("Images").child(user.getUid()).child(imageUri.getLastPathSegment());
                filePath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>()
                {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
                    {
                        Toast.makeText(getActivity(), "Uploaded Successfully!", Toast.LENGTH_SHORT).show();
                        Uri downloadUri = taskSnapshot.getDownloadUrl();

                        // Save image uri in the Firebase database under the usersID
                        myRef.child("Users").child(user.getUid()).child("profileImage").setValue(downloadUri.toString());
                        progressDialog.dismiss();
                    }
                }).addOnFailureListener(new OnFailureListener()
                {
                    @Override
                    public void onFailure(@NonNull Exception e)
                    {
                        progressDialog.dismiss();
                        Toast.makeText(getActivity(), "Failed To Upload!", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        return view;
    }

    // Get image data and display on page
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK)
        {
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setMessage("Displaying Image...");
            progressDialog.show();

            imageUri = data.getData();
            Picasso.get().load(imageUri).into(profileImage);

            progressDialog.dismiss();
        }
    }

2 个答案:

答案 0 :(得分:1)

您可以尝试将图像下载为Bitmap,然后旋转图像然后保存。以下是您可以使用Picasso执行此操作的代码:

int rotationAngle; // You set this angle before calling the method
Target target = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotationAngle);
        Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                matrix, true);

        // Save the rotatedBitmap elsewhere
    }
    @Override
    public void onBitmapFailed(Drawable errorDrawable) {}
    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
void downloadImageRotateAndSave() {
    Picasso.with(getContext()).load(imageUri).into(target);
}

答案 1 :(得分:0)

首先让我们看看是什么问题。首先,picaso打开图像文件并从中检索位图。此位图是显示在imageView中的内容。旋转图像时,您正在做的是旋转位图,但原始照片的文件永远不会改变。因此,如果要上传旋转照片,则必须从图像视图中检索新位图,使用它创建新文件并上传新文件。

必须要做的伎俩。

祝你好运