当我尝试使用downloadUrl.result
检索上载的downloadUrl(上传正常)时,应用程序崩溃。使用downloadUrl.toString()
时,该应用程序不会崩溃,但这并不能为我带来字符串形式的downloadUrl。我曾在论坛上读过人们有类似问题的论坛,但我无法解决。预先谢谢你
private fun uploadImageToFirebase(){
val profileImageRef = FirebaseStorage.getInstance().getReference("profilepics/" + System.currentTimeMillis() + ".jpg")
if (uriProfileImage != null) {
profileImageRef.putFile(uriProfileImage!!)
.addOnSuccessListener { taskSnapshot ->
profileImageUrl = taskSnapshot.metadata?.reference?.downloadUrl?.result
}
.addOnFailureListener { e ->
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
}
}
}
调试:
---------崩溃开始
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kelbel.frederik.coinz, PID: 17449
java.lang.IllegalStateException: Task is not yet complete
at com.google.android.gms.common.internal.Preconditions.checkState(Unknown Source)
at com.google.android.gms.tasks.zzu.zzb(Unknown Source)
at com.google.android.gms.tasks.zzu.getResult(Unknown Source)
at com.example.kelbel.frederik.coinz.FragmentSettings$uploadImageToFirebase$1.onSuccess(FragmentSettings.kt:121)
at com.example.kelbel.frederik.coinz.FragmentSettings$uploadImageToFirebase$1.onSuccess(FragmentSettings.kt:35)
at com.google.firebase.storage.StorageTask.zza(com.google.firebase:firebase-storage@@16.0.2:123)
at com.google.firebase.storage.zze.zza(com.google.firebase:firebase-storage@@16.0.2)
at com.google.firebase.storage.zzv.zza(com.google.firebase:firebase-storage@@16.0.2:89)
at com.google.firebase.storage.zzy.run(com.google.firebase:firebase-storage@@16.0.2)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
logcat:
2018-10-05 16:29:53.732 31185-31758/system_process W/ActivityManager: Force finishing activity com.example.kelbel.frederik.coinz/.ProfileActivity
2018-10-05 16:29:53.855 31185-18160/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
2018-10-05 16:29:53.855 31185-18160/system_process D/OpenGLRenderer: Swap behavior 1
2018-10-05 16:29:53.855 31185-18160/system_process W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2018-10-05 16:29:53.855 31185-18160/system_process D/OpenGLRenderer: Swap behavior 0
2018-10-05 16:29:53.876 31185-18160/system_process D/EGL_emulation: eglCreateContext: 0x96bc2160: maj 2 min 0 rcv 2
2018-10-05 16:29:53.907 31185-18160/system_process D/EGL_emulation: eglMakeCurrent: 0x96bc2160: ver 2 0 (tinfo 0x9246d0e0)
2018-10-05 16:29:53.915 31185-18160/system_process D/EGL_emulation: eglMakeCurrent: 0x96bc2160: ver 2 0 (tinfo 0x9246d0e0)
2018-10-05 16:29:54.246 31185-31207/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{5a43b89 u0 com.example.kelbel.frederik.coinz/.ProfileActivity t153 f}
2018-10-05 16:29:54.271 1311-1339/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 4784128
2018-10-05 16:29:54.296 1311-1311/? E/EGL_emulation: tid 1311: eglCreateSyncKHR(1962): error 0x3004 (EGL_BAD_ATTRIBUTE)
2018-10-05 16:29:54.399 31727-31930/com.google.android.googlequicksearchbox:search D/EGL_emulation: eglMakeCurrent: 0xa6474700: ver 2 0 (tinfo 0x92a170a0)
2018-10-05 16:29:54.517 31727-31727/com.google.android.googlequicksearchbox:search I/OptInState: There is a new client and it does not support opt-in. Dropping request.
2018-10-05 16:29:54.530 31727-17556/com.google.android.googlequicksearchbox:search W/LocationOracle: No location history returned by ContextManager
2018-10-05 16:29:54.538 31727-31727/com.google.android.googlequicksearchbox:search I/MicroDetectionWorker: Micro detection mode: [mDetectionMode: [1]].
2018-10-05 16:29:54.539 31727-31727/com.google.android.googlequicksearchbox:search I/AudioController: Using mInputStreamFactoryBuilder
2018-10-05 16:29:54.553 31727-17936/com.google.android.googlequicksearchbox:search I/MicroRecognitionRunner: Starting detection.
2018-10-05 16:29:54.562 31727-9036/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_starting com.google.android.apps.gsa.staticplugins.z.c@88e0935
2018-10-05 16:29:54.571 30990-17939/? I/AudioFlinger: AudioFlinger's thread 0xa7383e00 ready to run
2018-10-05 16:29:54.577 30990-2569/? E/AudioFlinger: not enough memory for AudioTrack size=131296
2018-10-05 16:29:54.578 30990-2569/? D/MemoryDealer: AudioTrack (0xae19cef0, size=4194304)
0: 0xae19cf00 | 0x00000000 | 0x000200E0 | A
1: 0xae19cf20 | 0x000200E0 | 0x000200E0 | A
2: 0xae19cdd0 | 0x000401C0 | 0x000200E0 | A
3: 0xae19cb90 | 0x000602A0 | 0x000200E0 | A
4: 0xad889180 | 0x00080380 | 0x000200E0 | A
5: 0xae1ff270 | 0x000A0460 | 0x000200E0 | A
6: 0xae1ff460 | 0x000C0540 | 0x000200E0 | A
7: 0xae1ff500 | 0x000E0620 | 0x000200E0 | A
8: 0xae1ff650 | 0x00100700 | 0x000200E0 | A
9: 0xae1ff980 | 0x001207E0 | 0x000200E0 | A
10: 0xae1ff9a0 | 0x001408C0 | 0x000200E0 | A
11: 0xae1ff820 | 0x001609A0 | 0x000200E0 | A
12: 0xae1ff860 | 0x00180A80 | 0x000200E0 | A
13: 0xad889350 | 0x001A0B60 | 0x000200E0 | A
14: 0xae1ffe40 | 0x001C0C40 | 0x000200E0 | A
15: 0xae1ffd10 | 0x001E0D20 | 0x000200E0 | A
16: 0xab0600d0 | 0x00200E00 | 0x000200E0 | A
17: 0xab060150 | 0x00220EE0 | 0x000200E0 | A
18: 0xab060450 | 0x00240FC0 | 0x000200E0 | A
19: 0xad8895b0 | 0x002610A0 | 0x000200E0 | A
2018-10-05 16:29:54.579 30990-2569/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
2018-10-05 16:29:54.579 31727-9036/com.google.android.googlequicksearchbox:search E/AudioRecord: AudioFlinger could not create record track, status: -12
2018-10-05 16:29:54.584 31727-9036/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
2018-10-05 16:29:54.584 31727-9036/com.google.android.googlequicksearchbox:search E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
2018-10-05 16:29:54.584 31727-9036/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_started com.google.android.apps.gsa.staticplugins.z.c@88e0935
2018-10-05 16:29:54.585 31727-9036/com.google.android.googlequicksearchbox:search E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
2018-10-05 16:29:54.586 31727-31727/com.google.android.googlequicksearchbox:search I/MicroDetectionWorker: onReady
2018-10-05 16:29:54.591 31706-31796/com.google.android.apps.nexuslauncher D/EGL_emulation: eglMakeCurrent: 0xb5b05420: ver 2 0 (tinfo 0xb5b033c0)
2018-10-05 16:29:54.620 31727-31746/com.google.android.googlequicksearchbox:search I/art: Background sticky concurrent mark sweep GC freed 4987(326KB) AllocSpace objects, 0(0B) LOS objects, 5% free, 18MB/19MB, paused 6.573ms total 64.756ms
2018-10-05 16:29:54.623 31727-9036/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_close com.google.android.apps.gsa.staticplugins.z.c@88e0935
2018-10-05 16:29:54.625 31727-17936/com.google.android.googlequicksearchbox:search I/MicroRecognitionRunner: Detection finished
2018-10-05 16:29:54.625 31580-31911/com.google.android.gms.persistent W/GCoreFlp: No location to return for getLastLocation()
2018-10-05 16:29:54.625 31727-17936/com.google.android.googlequicksearchbox:search W/ErrorReporter: reportError [type: 211, code: 524300]: Error reading from input stream
2018-10-05 16:29:54.626 31727-31988/com.google.android.googlequicksearchbox:search I/MicroRecognitionRunner: Stopping hotword detection.
2018-10-05 16:29:54.626 31727-17936/com.google.android.googlequicksearchbox:search W/ErrorProcessor: onFatalError, processing error from engine(4)
com.google.android.apps.gsa.shared.speech.a.g: Error reading from input stream
at com.google.android.apps.gsa.staticplugins.recognizer.i.a.a(SourceFile:342)
at com.google.android.apps.gsa.staticplugins.recognizer.i.a$1.run(SourceFile:1367)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:66)
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)
at com.google.android.apps.gsa.shared.util.concurrent.a.ad$1.run(SourceFile:85)
Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.
at com.google.android.apps.gsa.speech.audio.Tee.g(SourceFile:2531)
at com.google.android.apps.gsa.speech.audio.ap.read(SourceFile:555)
at java.io.InputStream.read(InputStream.java:101)
at com.google.android.apps.gsa.speech.audio.al.run(SourceFile:362)
at com.google.android.apps.gsa.speech.audio.ak$1.run(SourceFile:471)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:66)
at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:139)
at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:139)
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)
at com.google.android.apps.gsa.shared.util.concurrent.a.ad$1.run(SourceFile:85)
2018-10-05 16:29:54.626 31727-17936/com.google.android.googlequicksearchbox:search I/AudioController: internalShutdown
2018-10-05 16:29:54.631 31727-31727/com.google.android.googlequicksearchbox:search I/MicroDetector: Keeping mic open: false
2018-10-05 16:29:54.631 31727-31727/com.google.android.googlequicksearchbox:search I/MicroDetectionWorker: #onError(false)
2018-10-05 16:29:54.631 31727-17935/com.google.android.googlequicksearchbox:search I/DeviceStateChecker: DeviceStateChecker cancelled
2018-10-05 16:29:54.646 31580-31911/com.google.android.gms.persistent W/GCoreFlp: No location to return for getLastLocation()
2018-10-05 16:29:54.930 30990-31005/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 79985503 , only wrote 79832880
答案 0 :(得分:0)
我现在解决了。任务尚未完成。这就是为什么我无法检索downloadUrl的原因。添加了一个OnCompleteListener ...
taskSnapshot.storage.downloadUrl.addOnCompleteListener{
task -> if(task.isSuccessful){
profileImageUrl = task.result
}else{
Log.d("Crash", "Crash starts here")
}
答案 1 :(得分:0)
尝试一下:
storageReference.putFile(postImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful())
{
String downloadUri = task.getResult().getDownloadUrl().toString();
Map<String, Object> postMap = new HashMap<>();
postMap.put("image_download_uri",downloadUri);
postMap.put("post_title",title);
postMap.put("current_user_id",current_user_id);
postMap.put("current_time",currentTime.toString());
postMap.put("timestamp", FieldValue.serverTimestamp());