检查Firebase存储中是否存在具有特定名称的文件夹 [英] Check if a folder exists of a certain name in Firebase storage

查看:75
本文介绍了检查Firebase存储中是否存在具有特定名称的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firebase存储中,我有一个名为uploads/的文件夹.在该文件夹中,我有多个以注册用户的userId命名的文件夹.

In the Firebase Storage, I have a folder named uploads/. Inside that folder, I have multiple folders named after the userIds of the registered users.

这就是我想要做的.当用户输入"ProfileActivity"时,如果在uploads/文件夹下有任何以该用户的userId命名的文件夹,则代码将在Firebase数据库中查找.如果文件夹不存在,然后相应地运行代码.

This is what I'm trying to do. When the user enters the 'ProfileActivity', the code looks in the Firebase Database if there is any folder named after the userId of that user under the uploads/ folder. And then runs code accordingly if the folder exists or not.

这是我到目前为止所做的,但是在这里addValueEventListener尚未解决.我做对了吗?我在这里想念的是什么?

This is what I have done so far, but here the addValueEventListener is not resolved. Am I doing it the right way? What I'm missing here?

public class ProfileActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private StorageReference mStorageRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        mAuth = FirebaseAuth.getInstance();
        mStorageRef = FirebaseStorage.getInstance().getReference("uploads");
    }


    @Override
    protected void onStart() {
        super.onStart();
        FirebaseUser user = mAuth.getCurrentUser();
        String userID = user.getUid();
        if (user.isEmailVerified()) {
            StorageReference userFolder = mStorageRef.child(userID);
            userFolder.addValueEventListener(new ValueEventListener() {
                @Override
                void onDataChange(DataSnapshot snapshot) {
                    if (snapshot.exists()) {
                        // run some code
                    } else {
                        //Code
                    }
                }
            });

        }
    }
}

推荐答案

Firebase的Cloud Storage没有像实时数据库这样的事件列表器.您正在尝试执行不支持的操作(这就是为什么会出现编译器错误的原因).请参阅 StorageReference 的API文档.找出您可以使用该对象做什么.

Cloud Storage for Firebase doesn't have event listners like Realtime Database. You're trying to do something that isn't supported (which is why you're getting a compiler error). Please see the API documentation for StorageReference to find out what you can do with that object.

最重要的是,无法检查Cloud Storage中是否存在文件夹". Cloud Storage实际上没有任何文件夹.它只包含带有类似于文件夹的路径组件的文件.

On top of that, there is no way to check if a "folder" exists in Cloud Storage. Cloud Storage doesn't actually have any folders. It just has files with path components that look like folders.

如果您想知道文件是否存在(不是文件夹"),则可以使用

If you want to know if a file exists (not a "folder"), then you could use the getMetadata method on a StorageReference that refers to the file.

这篇关于检查Firebase存储中是否存在具有特定名称的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆