2017-08-10 249 views
1

我想在将图片上传到Firebase存储之前调整大小。Android Firebase调整图片大小并上传到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); 

      } 

和火力上传底座上的开放的我们像这样(如果有其他的方式来上传文件,它会如此有用):

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?或者只是调整大小并成功上传?

回答

0

如果您只需要上传特定尺寸的文件,以便您可以使用相同尺寸检索文件,则可以使用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' 

}

+0

我是新毕加索,所以我d喜欢知道如何从ViewHolder的ImageView中获取图像的Uri。 我明白,毕加索将调整图像大小,但我需要特定的Uri,这是调整大小的Imageview。和我知道如何访问SD卡上的原始图像文件或从Uri加载图像但我不知道如何使Uri是调整大小的图像。 – Jarvis

+0

对不起,我刚刚收到此消息。为什么你需要调整图像大小?毕加索允许您显示调整大小的图像,但是您是否需要存储具有特定大小的图像? – Anthony

+0

,因为我需要将图像上传到服务器(firebase存储) 我相信毕加索只显示图像是不是?如果毕加索可以调整(或压缩)源自身,这将会有所帮助。 – Jarvis

相关问题