简体   繁体   中英

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:

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. 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.

EDIT: I added onFailure and put a breakpoint there, it skips over that part too (didn't stop at the breakpoint)

my 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. Assuming you did that, the next thing I would check is adding an onFailure listener. Perhaps the loading is failing and you are not getting the message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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