简体   繁体   English

StorageReference getFile 无法正常工作

[英]StorageReference getFile doesn't work properly

I have a text file I'm storing on the Firebase database and I'm trying to retrieve the text as following:我有一个存储在 Firebase 数据库中的文本文件,我正在尝试按以下方式检索文本:

File tempFile = File.createTempFile("tempfile", ".txt");
    Path filename = tempFile.toPath();
    FileDownloadTask fdt = gsReference.getFile(tempFile);
    fdt.addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
            try {
                 wholeFile = new String(Files.readAllBytes(filename), StandardCharsets.UTF_8);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            Toast.makeText(MainActivity.this, "failed to read", Toast.LENGTH_LONG);
        }
    });

it worked once but now when debugging I see that the onSuccessListener is never called anymore, it skips right over to the next part of the code.它工作过一次,但现在在调试时我看到 onSuccessListener 不再被调用,它直接跳到代码的下一部分。 I've seen a similiar question where someone said the getFile method doesn't work properly with SDK 29 so I tried limiting mine to 28 but still no luck.我看到一个类似的问题,有人说 getFile 方法不能正常使用 SDK 29,所以我尝试将我的限制为 28,但仍然没有成功。

EDIT: I added onFailure and put a breakpoint there, it skips over that part too (didn't stop at the breakpoint)编辑:我添加了 onFailure 并在那里放置了一个断点,它也跳过了那部分(没有在断点处停止)

my build.gradle:我的 build.gradle:

dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'com.google.firebase:firebase-storage:20.0.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation platform('com.google.firebase:firebase-bom:29.3.1')
implementation 'com.google.firebase:firebase-analytics'}

There is some missing information here, that makes it unclear what the issue actually is.这里缺少一些信息,因此不清楚问题到底是什么。

First, I would make sure you have added the correct dependance to your app/build.gradle file.首先,我会确保您已将正确的依赖项添加到您的 app/build.gradle 文件中。 Assuming you did that, the next thing I would check is adding an onFailure listener.假设您这样做了,接下来我要检查的是添加一个 onFailure 侦听器。 Perhaps the loading is failing and you are not getting the message.也许加载失败,您没有收到消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM