替换不推荐使用的FireBase Storage downloadUrl

时间:2018-10-31 14:47:00

标签: android firebase firebase-storage

我的配置已被弃用,我试图弄清楚如何使其重新联机。不推荐使用的行是Uri firebaseURL = taskSnapshot.getDownloadUrl();,这是原始代码:

private void executeUploadTask(){
    showDialog();
    FilePaths filePaths = new FilePaths();
    //specify where the photo will be stored
    final StorageReference storageReference = FirebaseStorage.getInstance().getReference()
            .child(filePaths.FIREBASE_IMAGE_STORAGE + "/" + FirebaseAuth.getInstance().getCurrentUser().getUid()
                    + "/profile_image"); //just replace the old image with the new one

    if(mBytes.length/MB < MB_THRESHHOLD) {

        // Create file metadata including the content type
        StorageMetadata metadata = new StorageMetadata.Builder()
        .setContentType("image/jpg")
        .setContentLanguage("en") //see nodes below
            /*
            Make sure to use proper language code ("English" will cause a crash)
            I actually submitted this as a bug to the Firebase github page so it might be
            fixed by the time you watch this video. You can check it out at https://github.com/firebase/quickstart-unity/issues/116
            */
            .setCustomMetadata("MetaData")
            .setCustomMetadata("location", "Iceland")
            .build();
            //if the image size is valid then we can submit to database
            UploadTask uploadTask = null;
            uploadTask = storageReference.putBytes(mBytes, metadata);
            //uploadTask = storageReference.putBytes(mBytes); //without metadata


            uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    //Now insert the download url into the firebase database
                    Uri firebaseURL = taskSnapshot.getDownloadUrl();
                    Toast.makeText(SettingsActivity.this, "Upload Success", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "onSuccess: firebase download url : " + firebaseURL.toString());
                    FirebaseDatabase.getInstance().getReference()
                            .child(getString(R.string.dbnode_users))
                            .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                            .child(getString(R.string.field_profile_image))
                            .setValue(firebaseURL.toString());

                    hideDialog();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    Toast.makeText(SettingsActivity.this, "could not upload photo", Toast.LENGTH_SHORT).show();

                    hideDialog();

                }
            }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                    double currentProgress = (100 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                    if(currentProgress > (progress + 15)){
                        progress = (100 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                        Log.d(TAG, "onProgress: Upload is " + progress + "% done");
                        Toast.makeText(SettingsActivity.this, progress + "%", Toast.LENGTH_SHORT).show();
                    }

                }
            })
            ;
        }else{
            Toast.makeText(this, "Image is too Large", Toast.LENGTH_SHORT).show();
        }

    }

这是我当前的代码,但是没有上传到存储组件。我已经阅读了一些有关Stackoverflow的文章,但是还无法解决。

private void executeUploadTask(){
    showDialog();
    FilePaths filePaths = new FilePaths();
    //specify where the photo will be stored
    final StorageReference storageReference = FirebaseStorage.getInstance().getReference()
            .child(filePaths.FIREBASE_IMAGE_STORAGE + "/" + FirebaseAuth.getInstance().getCurrentUser().getUid()
                    + "/profile_image"); //just replace the old image with the new one

        if(mBytes.length/MB < MB_THRESHHOLD) {

            storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {

                    FirebaseDatabase.getInstance().getReference()
                            .child(getString(R.string.dbnode_users))
                            .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                            .child(getString(R.string.field_profile_image))
                            .setValue(uri.toString());
                }
            });


    }else{
        Toast.makeText(this, "Image is too Large", Toast.LENGTH_SHORT).show();
    }
}

调试时不会进入Override方法。我已经阅读了很多有关此的文章,但还没有使它起作用。我正在寻找应将顶部代码替换为什么以及流程的解释。

0 个答案:

没有答案