安卓Firebase:调整图像大小并上传到Firebase存储

4
我想在上传到 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个回答

1
如果你只需要上传指定大小的文件,以便能够在相同大小的情况下检索它,你可以使用Picasso http://square.github.io/picasso/。它允许你提供一个url并调整图像的高度和宽度尺寸,而不必在上传之前调整图像的大小(你可能因为其他原因想要这样做)。以下是我实现的一个示例,用于从URL获取图像并将其调整大小以适应我的ImageView。
/**
 * 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());

}

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

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.squareup.picasso:picasso:2.5.2' <!--Added Here-->
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'

}


我是Picasso的新手,想知道如何从ViewHolder的ImageView中获取图像的Uri。 我知道Picasso会调整图像大小,但我需要特定尺寸的Uri来加载已调整大小的Imageview。我也知道如何访问SD卡上的原始图像文件或从Uri中加载图像,但我不知道如何获得已调整大小的图像的Uri。 - Jarvis
因为我需要将图片上传到服务器(Firebase存储),所以我相信Picasso只是显示图片,对吗?如果Picasso能够自行调整大小(或压缩)源文件,那将会很有帮助。 - Jarvis
是的,它会获取图像并为您加载到 ImageView 中。您可以选择调整其大小。 - Anthony
我想知道这对你是否有帮助。我还没有设置Firebase存储,但是当我设置好后,我可以进行测试。https://dev59.com/ZG445IYBdhLWcg3wwcyg - Anthony
你的解决方案没有解决这个问题。 - MobDev
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接