2017-04-25 58 views
1

我試圖將圖片從圖庫上傳到Firebase存儲。將圖片上傳到Firebase存儲失敗

從圖庫中選取圖片效果不錯,但所選圖片未上傳到Firebase存儲。

我認爲問題出在我的代碼中,我無法弄清楚這個模塊。

if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) { 

    Uri selectedImageUri = data.getData(); 

    // Get a reference to store file at photos/<FILENAME> 
    StorageReference photoRef = mPhotosStorageReference.child(selectedImageUri.getLastPathSegment()); 

    //Upload file to Firebase Storage 
    photoRef.putFile(selectedImageUri) 
    .addOnSuccessListener(this, new OnSuccessListener <UploadTask.TaskSnapshot>() { 
     @Override 
     public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 

     // When the image has successfully uploaded, we get its download URL 
     @SuppressWarnings("VisibleForTests") 
     Uri downloadUrl = taskSnapshot.getDownloadUrl(); 

     // Set the download URL to the message box, so that the user can send it to the database 
     Message message = new Message(null, userName, downloadUrl.toString()); 
     messagesDatabaseReference.push().setValue(message); 
     } 
    }); 

我正在通過USB在我的移動設備Moto G4(Android Nougat)上運行我的應用程序。

回答

0

在我OnActivityResult,我修改了以下內容:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == RC_SIGN_IN) { 
    if (resultCode == RESULT_OK) { 
     .... 
    } else if (resultCode == RESULT_CANCELED) { 
     .... 
    } 
    } else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) { 

    } 
} 
相關問題