taskSnapshot.getDownloadUrl() is deprecated

前端 未结 10 1457
时光说笑
时光说笑 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: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 urlTask = uploadTask.continueWithTask(new Continuation>() {
        @Override
        public Task then(@NonNull Task 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() {
        @Override
        public void onComplete(@NonNull Task 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() {
            @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() {
            @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() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // Handle successful uploads on complete
                // ...
            }
        });
    

提交回复
热议问题