当用户点击我的聊天应用中的某个按钮时,我尝试将图片上传到firebase存储空间,但它无效。
以下是上传代码
if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment());
photoRef.putFile(selectedImageUri)
.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// When the image has successfully uploaded, we get its download URL
Uri downloadUrl = taskSnapshot.getDownloadUrl();
// Set the download URL to the message box, so that the user can send it to the database
FriendlyMessage friendlyMessage = new FriendlyMessage(null, mUsername, downloadUrl.toString());
mDatabaseReference.push().setValue(friendlyMessage);
}
});
}
答案 0 :(得分:2)
public void uploadFile(Uri filePath, UploadImageInterface uploadImageInterface) {
StorageReference mStorageRef = FirebaseStorage.getInstance().getReferenceFromUrl(Constants.FIREBASE_STORAGE_PATH).child("images");
if (filePath != null) {
StorageReference filepathRef = mStorageRef.child("pic" + CommonUtilities.getTimeStampLong() + ".JPG");
filepathRef.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String imageUrl = taskSnapshot.getDownloadUrl() + "";
uploadImageInterface.onSuccess(imageUrl);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
uploadImageInterface.onFailure(exception.getMessage())
}
});
}
}