代码: @Override受保护的void onActivityResult(int requestCode,int resultCode,Intent data){super.onActivityResult(requestCode,resultCode,data);如果(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公共无效onSuccess(UploadTask.TaskSnapshot taskSnapshot){}});photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener< Uri>(){@Override公共无效onSuccess(Uri uri){字符串downloadUrl = uri.toString(); Log.d("tag",downloadUrl);FriendlyMessage friendlyMessage =新的FriendlyMessage(null,mUsername,downloadUrl);mMessagedatabaseReference.push().setValue(friendlyMessage);}}); 完整代码 解决方案在文件完成上传之前,您正在调用 getDownloadUrl(). putFile()是异步的,这意味着它会立即返回,并且每当上传完成后,您的回调就会在一段时间后被调用.您的代码假设上传要么立即完成,要么阻止当前线程,直到上传完成.这是行不通的.相反,当您知道上传已完成时,应该在回调中调用getDownloadUrl(). photoRef.putFile(selectedImageUri).addOnSuccessListener(new OnSuccessListener< UploadTask.TaskSnapshot>(){@Override公共无效onSuccess(UploadTask.TaskSnapshot taskSnapshot){photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener< Uri>(){@Override公共无效onSuccess(Uri uri){字符串downloadUrl = uri.toString();Log.d("tag",downloadUrl);FriendlyMessage friendlyMessage =新的FriendlyMessage(null,mUsername,downloadUrl);mMessagedatabaseReference.push().setValue(friendlyMessage);}});}}); I am building a chat app i have a list view setup that loads messages and i have a image picker setup to upload images to firebase and display them in the list view. When i go to upload an image i select the image from a image picker, it gets sucesfuly uploaded to firestore but not displayed and no message object created for it this is the error2019-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)but if i go an upload the same image right after the image will sucesfuly upload and displayI have it setup so that the image will be uploaded to firebase storage and then be displayed in the realtime database under the messages object as the image url Databasecode: @Overrideprotected 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); } });full code 解决方案 You're calling getDownloadUrl() before the file is finished uploading.putFile() is asynchronous, meaning it returns immediately, and your callback is invoked some time later, whenever the upload is done. Your code is assuming that the upload either completes immediately, or blocks the current thread until the upload is done. This isn't going to work.What you should do instead is call getDownloadUrl() inside the callback, when you know the upload is complete. 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); } }); } }); 这篇关于Firebase必须上传两次图像才能显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-29 21:55