taskSnapshot.getDownloadUrl() is deprecated

前端 未结 10 1382
时光说笑
时光说笑 2020-11-27 19:39

Until now, the way to get the url from file on Storage in Firebase, I used to do this taskSnapshot.getDownloadUrl, but nowadays is deprecated, which method I

相关标签:
10条回答
  • 2020-11-27 20:06

    Use taskSnapshot.getTask() instead of taskSnapshot.getDownloadUrl() works fine for me.

    0 讨论(0)
  • 2020-11-27 20:12
    //Create an instance of StorageReference first (here in this code snippet, it is storageRef)  
    
    StorageReference filepath = storageRef.child("images.jpg");
    
     //If file exist in storage this works.
     filepath.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
                                @Override
                                public void onComplete(@NonNull Task<Uri> task) {
                                    String downloadUrl = task.getResult().toString();
                                    // downloadurl will be the resulted answer
                                 }
     });
    
    0 讨论(0)
  • 2020-11-27 20:17

    You can use StorageReference.getDownloadUrl(). Please note that it returns a Task, so you will have to remember to treat it asynchronously like you do any other Task.

    0 讨论(0)
  • 2020-11-27 20:18

    Use this implementation for your uploadTask and will work. :)

    uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Task<Uri> firebaseUri = taskSnapshot.getStorage().getDownloadUrl();
                    firebaseUri.addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
    
                            String url = uri.toString();
                            Log.e("TAG:", "the url is: " + url);
    
                            String ref = yourStorageReference.getName();
                            Log.e("TAG:", "the ref is: " + ref);
                        }
                    });
                }
            });
    
    0 讨论(0)
  • 2020-11-27 20:22
    Task<Uri> task = taskSnapshot.getMetadata().getReference().getDownloadUrl();
    task.addOnSuccessListener(new OnSuccessListener<Uri>() {
           @Override
            public void onSuccess(Uri uri) {
                  String photoLink = uri.toString();
            }
    });
    
    0 讨论(0)
  • 2020-11-27 20:23

    You wont get the download url of image now using

    ImageUrl = taskSnapshot.getDownloadUrl().toString(); this method is deprecated.

    Instead you can use the below method

    uniqueId = UUID.randomUUID().toString();
    ur_firebase_reference = storageReference.child("user_photos/" + uniqueId);
    
    Uri file = Uri.fromFile(new File(mphotofile.getAbsolutePath()));
    UploadTask uploadTask = ur_firebase_reference.putFile(file);
    
    Task<Uri> urlTask = uploadTask.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 ur_firebase_reference.getDownloadUrl();
        }
    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {
            if (task.isSuccessful()) {
                Uri downloadUri = task.getResult();
                System.out.println("Upload " + downloadUri);
                Toast.makeText(mActivity, "Successfully uploaded", Toast.LENGTH_SHORT).show();
                if (downloadUri != null) {
    
                    String photoStringLink = downloadUri.toString(); //YOU WILL GET THE DOWNLOAD URL HERE !!!!
                    System.out.println("Upload " + photoStringLink);
    
                }
    
            } else {
                // Handle failures
                // ...
            }
        }
    });
    

    You can track the progress of uploading by adding Progress listeners,

    uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                System.out.println("Upload is " + progress + "% done");
                Toast.makeText(mContext, "Upload is " + progress + "% done", Toast.LENGTH_SHORT).show();
            }
        }).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
                System.out.println("Upload is paused");
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // Handle successful uploads on complete
                // ...
            }
        });
    
    0 讨论(0)
提交回复
热议问题