2017-08-10 247 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

相關問題