我建立一个聊天程序,我有一个加载邮件列表视图设置和我有一个图片选择器设置上传图片到火力点,并在列表视图中显示出来。当我去上传图片,我选择从图像拾取图像时,它会sucesfuly上传到公司的FireStore,但不会显示并为它这创造任何消息的对象是错误
2019-02-06 16:32:19.032 3810-5272/com.example.android.debateapp E/StorageException: { "error": { "code": 404, "message": "Not Found. Could not get object" }}
java.io.IOException: { "error": { "code": 404, "message": "Not Found. Could not get object" }}
at com.google.firebase.storage.obfuscated.zzj.zza(com.google.firebase:firebase-storage@@16.0.2:455)
at com.google.firebase.storage.obfuscated.zzj.zza(com.google.firebase:firebase-storage@@16.0.2:3435)
at com.google.firebase.storage.obfuscated.zzc.zza(com.google.firebase:firebase-storage@@16.0.2:65)
at com.google.firebase.storage.obfuscated.zzc.zza(com.google.firebase:firebase-storage@@16.0.2:57)
at com.google.firebase.storage.zzc.run(com.google.firebase:firebase-storage@@16.0.2:68)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
但如果我去上传同一图像后,右侧的图像将成功地上传和显示
我有它设置,使图像将被上传到firebase storage然后根据消息作为图片网址Database对象显示在实时数据库
码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICK && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment());
photoRef.putFile(selectedImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
}
});
photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String downloadUrl = uri.toString();Log.d("tag", downloadUrl);
FriendlyMessage friendlyMessage = new FriendlyMessage(null, mUsername, downloadUrl);
mMessagedatabaseReference.push().setValue(friendlyMessage);
}
});
文件上传完毕之前,你打电话getDownloadUrl()
。
putFile()
是异步的,也就是说它立即返回,和你的回调一段时间后调用,每当上传完成。您的代码是假设直到上载完成上传要么立即完成,或阻塞当前线程。这是行不通的。
你应该做的,而不是为调用getDownloadUrl()回调里面,当你知道上传完成。
photoRef.putFile(selectedImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String downloadUrl = uri.toString();
Log.d("tag", downloadUrl);
FriendlyMessage friendlyMessage = new FriendlyMessage(null, mUsername, downloadUrl);
mMessagedatabaseReference.push().setValue(friendlyMessage);
}
});
}
});