我在为DownloadUrl()添加替换代码时遇到了很大的麻烦[重复]

问题描述 投票:-2回答:1

任何帮助将不胜感激! (与DownloadURL()不同,后者已弃用)

当我更新Android Studio并将所有代码移至AndroidX材质设计支持库时,问题就开始了。 (没想到会有那么多重建)

这是我当前的设置:

        Log.d(TAG, "uploadNewPhoto: attempting to upload new photo");

        FilePaths filePaths = new FilePaths();
        //Case 1 new photo
        if (photoType.equals(mContext.getString(R.string.profile_photo))){

            String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
            StorageReference storageReference = mStorageReference
                    .child(filePaths.FIREBASE_IMAGE_STORAGE + "/" + user_id + "/photo" + (count + 1));

            //Convert image url to bitmap
            Bitmap bm = ImageManager.getBitmap(imgUrl);
            byte[] bytes = ImageManager.getBytesFromBitmap(bm, 100);

            UploadTask uploadTask = null;
            uploadTask = storageReference.putBytes(bytes);

            uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    ****Uri filebaseUrl = taskSnapshot.getDownloadUrl();****

                    Toast.makeText(mContext, "Photo Upload Success!", Toast.LENGTH_SHORT).show();

                    //add new photo to photos node and user_photos node

                    //navigate to main feed so user can see photo


                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.d(TAG, "onFailure: Photo upload failed.");
                    Toast.makeText(mContext, "Photo upload failed. ", Toast.LENGTH_SHORT).show();


                }
            }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onProgress(@NonNull UploadTask.TaskSnapshot taskSnapshot) {
                    double progress = (100 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();

                    if (progress - 15 > mPhotoUploadProgress){
                        Toast.makeText(mContext, "photo upload progress: " + String.format("%.0f", progress) + "%", Toast.LENGTH_SHORT).show();
                        mPhotoUploadProgress = progress;
                    }

                }
            });

        }else if (photoType.equals(mContext.getString(R.string.profile_photo))){

        }
        //case 2 new profile photo

    }
java android firebase firebase-storage
1个回答
0
投票

找到相关问题的解决方案->

替换:

Uri filebaseUrl = taskSnapshot.getDownloadUrl();

使用

Task<Uri> firebaseUri = taskSnapshot.getStorage().getDownloadUrl();
© www.soinside.com 2019 - 2024. All rights reserved.