Firebase Storage “retrieves a long lived download URL” using getDownloadUrl() now deprecated

落爺英雄遲暮 提交于 2019-11-26 21:30:17

问题


This getDownloadUrl() method showed deprecated after updating to

'com.google.firebase:firebase-storage:15.0.2'

Nothing on the official site to another way to achieve the url, So there's any way to achieve the Url in non deprecated way?

/** @deprecated */
@Deprecated
@Nullable
public Uri getDownloadUrl() {
    StorageMetadata var1;
    return (var1 = this.getMetadata()) != null ? var1.getDownloadUrl() : null;
    }
}

回答1:


In the docs it says this:

The getDownloadUrl() and getDownloadUrls() methods of the StorageMetadata class are now deprecated. Use getDownloadUrl() from StorageReference instead.

So you need to use getDownloadUrl() that is inside the StorageReference

public Task<Uri> getDownloadUrl ()

Asynchronously retrieves a long lived download URL with a revokable token. This can be used to share the file with others, but can be revoked by a developer in the Firebase Console if desired.

more information here:

https://firebase.google.com/docs/reference/android/com/google/firebase/storage/StorageReference#getDownloadUrl()




回答2:


  final StorageReference filePath = mImageStore.child("profile_images").child("full_image").child(userId + ".jpg");
                filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        //Bitmap hochladen
                        uploadBitMap(uri.toString());
                    }
                });strong text

Or

final UploadTask uploadTask = thumb_file.putBytes(thumb_bite);
        uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                //Url laden
                taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        Map imageUrls = new HashMap();
                        imageUrls.put("image", fullImageUrl);
                        imageUrls.put("thumb_image", uri.toString());
                        //In database
                        mAlarmsDatabaseReference.updateChildren(imageUrls).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                //Progressbar beende + Bild wieder anzeigen
                                progressBar.setVisibility(View.GONE);
                                circleProfilePicture.setVisibility(View.VISIBLE);

                                if(task.isSuccessful()){
                                    Toast.makeText(SettingsActivity.this, getResources().getString(R.string.ProfilbildUpdate), Toast.LENGTH_SHORT).show();
                                }else{
                                    Toast.makeText(SettingsActivity.this, "FAILED", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                });
            }
        });



回答3:


final UploadTask uploadTask = thumb_file.putBytes(thumb_bite); uploadTask.addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            //Url laden
            taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    Map imageUrls = new HashMap();
                    imageUrls.put("image", fullImageUrl);
                    imageUrls.put("thumb_image", uri.toString());
                    //In database
                    mAlarmsDatabaseReference.updateChildren(imageUrls).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            //Progressbar beende + Bild wieder anzeigen
                            progressBar.setVisibility(View.GONE);
                            circleProfilePicture.setVisibility(View.VISIBLE);

                            if(task.isSuccessful()){
                                Toast.makeText(SettingsActivity.this, getResources().getString(R.string.ProfilbildUpdate), Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(SettingsActivity.this, "FAILED", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            });
        }
    });


来源:https://stackoverflow.com/questions/50158921/firebase-storage-retrieves-a-long-lived-download-url-using-getdownloadurl-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!