Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why my getDownloadUrl() has error ? #196

Open
adibsyahmi17 opened this issue May 26, 2022 · 5 comments
Open

Why my getDownloadUrl() has error ? #196

adibsyahmi17 opened this issue May 26, 2022 · 5 comments

Comments

@adibsyahmi17
Copy link

@OverRide
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@OverRide
public void run() {
mProgressBar.setProgress(0);
}
}, 500);

                        Toast.makeText(AddSolatPage.this, "Upload successful", Toast.LENGTH_LONG).show();
                        Upload upload = new Upload(solatTitle.getText().toString().trim(),
                                taskSnapshot.getDownloadUrl().toString());

                    }
@thatfiredev
Copy link
Member

@adibsyahmi17 unfortunately, TaskSnapshot does not have a getDownloadUrl() method. You'll need to call that method on the StorageReference that you created to upload the file.

@adibsyahmi17
Copy link
Author

@adibsyahmi17 unfortunately, TaskSnapshot does not have a getDownloadUrl() method. You'll need to call that method on the StorageReference that you created to upload the file.

can you show me how because this is my first time using android studio

@thatfiredev
Copy link
Member

@adibsyahmi17 Can you show the code where you create the StorageReference? The one that comes before this onSuccess code that you posted here.

@adibsyahmi17
Copy link
Author

if (imageURI != null){
StorageReference fileReference = storageReference.child(System.currentTimeMillis()
+ "." + getFileExtension(imageURI));
fileReference.putFile(imageURI)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@OverRide
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@OverRide
public void run() {
mProgressBar.setProgress(0);
}
}, 500);

                        Toast.makeText(AddSolatPage.this, "Upload successful", Toast.LENGTH_LONG).show();
                        Upload upload = new Upload(solatTitle.getText().toString().trim(),
                                taskSnapshot.getStorage().getDownloadUrl().toString());

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(AddSolatPage.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(@NonNull UploadTask.TaskSnapshot snapshot) {
                        double progress = (100.0 * snapshot.getBytesTransferred() / snapshot.getTotalByteCount());
                        mProgressBar.setProgress((int) progress);
                    }
                });
    }else {
        Toast.makeText(this,"No file selected",Toast.LENGTH_SHORT).show();
    }

this is my code

@thatfiredev
Copy link
Member

@adibsyahmi17 Thanks for providing the code. You'll need to change your onSuccess method to reuse the StorageReference that you created:

@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            mProgressBar.setProgress(0);
        }
    }, 500);

    Toast.makeText(AddSolatPage.this, "Upload successful", Toast.LENGTH_LONG).show();
    fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            String url = uri.toString();
            Upload upload = new Upload(solatTitle.getText().toString().trim(), url);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants