Android firebase调整图像大小并上传到firebase存储

时间:2017-08-10 18:08:18

标签: android firebase firebase-storage

我希望在将图像上传到firebase存储之前调整图像大小。

我的问题是:FireBase基于其Uri上传图像。 我从图库意图中获取了图像并将其放在ImageView上并按照以下方式调整大小:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent returnIntentData) {
    super.onActivityResult(requestCode, resultCode, returnIntentData);

    switch(requestCode) {
        case RC_SELECT_PHOTO:
            if (resultCode == RESULT_OK) {

                mImageUri = returnIntentData.getData();
                mSelectedImage.setImageURI(mImageUri);

                //get a Image obj from ImageView

                Bitmap bit = ((BitmapDrawable) mSelectedImage.getDrawable()).getBitmap();

                int width = bit.getWidth();
                int height = bit.getHeight();
                float rate = 0.0f;

                int maxResolution = 1920;

                int newWidth = width;
                int newHeight = height;

                if(width > height) {
                    if(maxResolution < width) {
                        rate = maxResolution/ (float) width;
                        newHeight = (int) (height * rate);
                        newWidth = maxResolution;
                    }

                } else {
                    if(maxResolution < height) {
                        rate = maxResolution/(float)height;
                        newWidth = (int) (width * rate);
                        newHeight = maxResolution;
                    }
                }

                Bitmap resizedImage = Bitmap.createScaledBitmap(bit, newWidth, newHeight, true);

                mSelectedImage.setImageBitmap(resizedImage);

            }

和firebase上传它基于Uri就像这样(如果有其他方式上传文件,它会非常有帮助!):

public void uploadPhotoToStorage() {
    //present image's Ref
    StorageReference presentGymPhotoRef =
            mGymStorageReferences.child(mImageUri.getLastPathSegment());
    //Upload file to Firebase Storage
    presentGymPhotoRef.putFile(mImageUri)
            .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Uri downloadUri = taskSnapshot.getDownloadUrl();
                    //save gymPhoto Uri to instance

                    gymPhotoUri = downloadUri.toString();
                    gymName = mEditGymName.getText().toString();

.......

有没有办法获得Bitmap的Uri或ImageView的Uri?或者只是调整大小并成功上传?

1 个答案:

答案 0 :(得分:0)

如果您只需要以特定尺寸上传文件,以便以相同的尺寸检索文件,则可以使用Picasso http://square.github.io/picasso/。它允许您提供网址并使用您可能需要的高度和宽度尺寸调整图像大小,而无需在上传之前调整图像大小(您可能由于其他原因而想要)。下面是我实现的一个示例,用于从URL获取图像并调整图像大小以适合我的ImageView。

print(string % tuple(agrs))

它易于实现,请查看下面的代码,了解如何将其添加到项目中的示例:

/**
 * Binds the data from the json file to the ImageView and the Textviews
 * are part of the movie_layout resource file.
 * @param recyclerViewHolder
 * @param position
 */
@Override
public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int position) {
    MovieData movieData = list.get(position);
    Picasso.with(context).load(movieData.getMovie_img_url()).resize(75,100).into(recyclerViewHolder.imageView);
    recyclerViewHolder.textViewTitle.setText(movieData.getMovie_title());
    recyclerViewHolder.textViewAuthor.setText(movieData.getMovie_author());

}

}