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

Android firebase调整图像大小并上载到firebase存储,android,firebase,firebase-storage,Android,Firebase,Firebase Storage,我想在将图像上载到firebase存储之前调整其大小 我的问题是:FireBase根据其Uri上传图像。 我从gallery intent中获取图像,并将其放置在ImageView上,然后按如下方式调整其大小: @Override protected void onActivityResult(int requestCode, int resultCode, Intent returnIntentData) { super.onActivityResult(requestCode, re

我想在将图像上载到firebase存储之前调整其大小

我的问题是:FireBase根据其Uri上传图像。 我从gallery intent中获取图像,并将其放置在
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);

            }
@覆盖
ActivityResult上受保护的void(int-requestCode、int-resultCode、Intent-returnIntentData){
super.onActivityResult(请求代码、结果代码、返回意向数据);
开关(请求代码){
案例RC_选择_照片:
if(resultCode==RESULT\u OK){
mimageri=returnIntentData.getData();
mSelectedImage.setimageri(mimageri);
//从ImageView获取图像对象
位图位=((BitmapDrawable)MSelecteImage.getDrawable()).getBitmap();
int width=bit.getWidth();
int height=bit.getHeight();
浮动率=0.0f;
int最大分辨率=1920;
int newWidth=宽度;
int newHeight=高度;
如果(宽度>高度){
if(最大分辨率<宽度){
速率=最大分辨率/(浮动)宽度;
新高度=(整数)(高度*比率);
newWidth=最大分辨率;
}
}否则{
if(最大分辨率<高度){
速率=最大分辨率/(浮动)高度;
新宽度=(整数)(宽度*速率);
新高度=最大分辨率;
}
}
Bitmap resizedImage=Bitmap.createScaledBitmap(位、新宽度、新高度、真);
mSelectedImage.setImageBitmap(resizedImage);
}
firebase根据Uri像这样上传它(如果有其他上传文件的方法,它将非常有用!):

public void上传PhotoToStorage(){
//当前图像的参考
StorageReference Presentf=
mGymStorageReferences.child(mimageri.getLastPathSegment());
//将文件上载到Firebase存储
presentGymPhotoRef.putFile(mimageri)
.addOnSuccessListener(此,新的OnSuccessListener(){
@凌驾
成功时公共无效(UploadTask.TaskSnapshot TaskSnapshot){
Uri downloadUri=taskSnapshot.getDownloadUrl();
//将照片Uri保存到实例
gymPhotoUri=downloadUri.toString();
gymName=mEditGymName.getText().toString();
.......

有没有办法获取
位图的Uri或
图像视图的Uri?或者只需调整大小并成功上载此文件?

如果您只需要上载特定大小的文件,以便可以检索与毕加索相同大小的文件。它允许您提供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'
依赖项{
编译文件树(目录:“libs”,包括:['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso核心:2.2.2'{
排除组:“com.android.support”,模块:“support annotations”
})
编译'com.android.support:appcompat-v7:25.3.1'
编译'com.android.support:recyclerview-v7:25.3.1'
编译'com.squareup.picasso:picasso:2.5.2'
编译'com.android.support.constraint:constraint布局:1.0.2'
testCompile'junit:junit:4.12'

}

我是毕加索的新人,所以我想知道如何从ViewHolder的ImageView中获取图像的Uri。我知道毕加索将调整图像大小,但我需要调整大小的ImageView的特定Uri。我知道如何访问sd卡上的原始图像文件或从Uri加载图像,但我不知道如何制作调整大小的图像的Uri。对不起,我刚刚收到了这个消息。为什么你需要调整图像大小?毕加索允许你显示调整大小的图像,但你需要存储具有特定大小的图像吗?因为我需要将图像上载到服务器(firebase存储),我相信毕加索只显示图像,不是吗?如果毕加索可以调整大小(或压缩)源本身会很有帮助。是的,它会为您获取图像并在ImageView中加载图像。您可以根据自己的选择调整其大小。我要求的原因是您已经存储了图像,并且此过程将允许您显示调整大小的图像,这样就不需要调整图像大小来存储它。我想您可以o从内存中下载,关于stackoverflow如何实现这一点,有一些答案,但如果您能给我提供更多的背景信息,这将非常有用,因为我计划使用firebase存储来存储图像并在应用程序上显示它们。
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'