Click here to Skip to main content
14,981,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
With the update, .getDownloadUrl doesn't work anymore so I was wondering how I can change it in my code specifically

Java
<pre><pre> <pre> private void executeUploadTask(){

        Toast.makeText(getActivity(), "Uploading Image", Toast.LENGTH_SHORT).show();
        final String postId = FirebaseDatabase.getInstance().getReference().push().getKey();
        final StorageReference storageReference = FirebaseStorage.getInstance().getReference()
                .child("uploads/users" + FirebaseAuth.getInstance().getCurrentUser()
                        .getUid()+"/" + postId + "/upload_image");

        UploadTask uploadTask = storageReference.putBytes(mUploadBytes);
        uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Toast.makeText(getActivity(), "Filed", Toast.LENGTH_SHORT).show();

                Uri firebaseUri = taskSnapshot.getDownloadUrl();

                Log.d(TAG, "onSuccess: firebase download url: " + firebaseUri.toString());

                DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

                //things that should be in the database
                Post post = new Post();
                post.setImage(firebaseUri.toString());
                post.setDate(mDateOfUse.getText().toString());
                post.setFileName(mFileName.getText().toString());
                post.setGrade(mGrade.getText().toString());
                post.setNameOfClass(mClass.getText().toString());
                post.setPageNumber(mPage.getText().toString());
                post.setReflection(mReflection.getText().toString());
                post.setPost_id(postId);
                post.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
                post.setRatingBar(Float.toString(mRatingBar.getRating()));

                reference.child(getString(R.string.node_post))
                        .child(postId)
                        .setValue(post);

                resetFields();

            }


}

}

What I have tried:

I tried to implement
<pre lang="java">Task<Uri> urlTask = storageReference.getDownloadUrl().urlTask.addOnSuccessListener(new OnSuccessListener<>())
but I don't know where to put it in my code. Please help me, I am really desperate.
Posted
Updated 29-Feb-20 13:27pm
v2

change this
Uri downloadUrl = taskSnapshot.getDownloadUrl();

to this

Task<uri> downloadUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl();
   
you can use this instead
Java
<pre> final StorageReference ref = mStorageRef.child("folder_name").child(imageUri.getLastPathSegment());
            //uploadTask = ref.putFile(filepa);

            Task<Uri> urlTask = ref.putFile(imageUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                @Override
                public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                    if (!task.isSuccessful()) {
                        throw task.getException();
                    }

                    // Continue with the task to get the download URL
                    return ref.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    if (task.isSuccessful()) {
                        Uri downloadUrl = task.getResult();
                        mProgress.dismiss();
                    } else {
                        // Handle failures
                        // ...
                    }
                }
            });


it work for me
   
Solved: I changed
Java
Uri firebaseUri = taskSnapshot.getDownloadUrl();


to

Java
Task<Uri> firebaseUri = taskSnapshot.getStorage().getDownloadUrl();
   
v2
Comments
Member 14126339 22-Jan-19 13:23pm
   
thanks man!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900