我尝试将图片从图库上传到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 :(得分: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) {
}
}