检查URL是否存在带有Datasnapshot的Firebase数据库 [英] To check if URL exists Firebase Database with Datasnapshot

查看:93
本文介绍了检查URL是否存在带有Datasnapshot的Firebase数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有一个图像,该图像具有预定义的路径.我将使用Datasnapshot进行查询,看看路径是否是我确定的路径,然后继续下一步,它将负责保存新照片,而不会删除旧照片(默认图像并保留在Firebase存储中) .如果不是预期的URL,则请执行删除Firebase图像的过程.

I have an image in the database, which has a predefined path. I would conduct the query with Datasnapshot and see if the path is the path that I determine, then move on to the next step, which will be responsible for saving a new photo without deleting the old one which is default image and stays in Firebase Storage. If it is not the expected URL, then yes, execute the process that deletes the firebase image.

当前,您正在忽略我的查询,并且已经进入下一步,即删除图像.如何检查此URL,以及是否没有任务?

Currently you are ignoring my query and are already going to the next step which is to remove the image. How can I check this URL and if there is no task?

图像数据库:

代码:

*fotoEmpresaDB = FirebaseDatabase.getInstance().getReference().child("Empresas").child(user_id).child("foto_capa");*/
            fotoEmpresaDB.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {


                    if ( dataSnapshot.equals("https://firebasestorage.googleapis.com/v0/b/name-database/Default%2Fprofiledefault.jpg?alt=media&token=ad1ce6b7-1195-40fa-a88a-affade7517b0") ){

                        /*Method that saves image without deleting the previous image*/
                        salvarDadosImagem();




                    } else {

                        mProgress.setMessage("Deleting image....");
                        mProgress.show();
                        mProgress.setCancelable(false);

                        String url = dataSnapshot.getValue(String.class);
                        mStorageUrl = FirebaseStorage.getInstance().getReferenceFromUrl(url);
                        mStorageUrl.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {

                                salvarDadosImagem();




                            }

我尝试过:datasnapshot.hashChild("URL").

I have tried as: datasnapshot.hashChild("URL").

发生此错误:Firebase数据库路径无效:Firebase数据库路径不得包含.",#","$","["或]"

This error happens: Invalid Firebase Database path: Firebase Database paths must not contain '.', '#', '$', '[', or ']'

推荐答案

不太确定我了解您要执行的操作,但这是两个最可能的答案.

Not really sure I understand what you're trying to do, but here are the two most likely answers.

  1. 要检测该值是否与您要查找的dataSnapshot.getValue(String.class).equals("https://REST_OF_YOUR_URL")

要检测节点是否具有任何值(因此,如果存在):dataSnapshot.exists()

To detect if the node has any value (so: if it exists): dataSnapshot.exists()

这篇关于检查URL是否存在带有Datasnapshot的Firebase数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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