Firebase存储-未创建链接 [英] Firebase Storage - Link not created

查看:55
本文介绍了Firebase存储-未创建链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将图像和文档上载到Firebase存储器,并将两者创建的链接上载到Real-Time DB,所有这些操作都在单击按钮时发生. 但是在firebase数据库中,链接没有上传,而我和他们一起上传的其他文本在那里.

I am trying to upload an image and a document to the firebase storage and upload the links created from both to the Real-Time DB, everything happening on a button's click. But in the firebase DB, the links are not uploaded while the other text I upload along with them is there.

存储上传代码:

updata.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //before uploading both the doc and img..
            progressDialog.setTitle("Uploading Images");
            progressDialog.show();

            final UploadData uploadData=new UploadData();

            if(ImgPathUri!=null){
                StorageReference str=storageReference.child(StoragePath + System.currentTimeMillis() + "." + getExtension(ImgPathUri));

                str.putFile(ImgPathUri)
                        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

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

                                                String ImgLink=uri.toString();
                                                linkimg=ImgLink;
                                                uploadData.setImgURL(linkimg);

                                            }
                                        });

                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Toast.makeText(addEventActivity.this, "fucked ra", Toast.LENGTH_SHORT).show();
                                finished=false;
                            }
                        })
                        .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

                            }
                        });
            }

            if(DocPathUri!=null){
                StorageReference storageReference1=storageReference.child(StoragePath + System.currentTimeMillis() + "." + getExtension(DocPathUri));

                storageReference1.putFile(DocPathUri)
                        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                                taskSnapshot.getStorage().getDownloadUrl()
                                        .addOnSuccessListener(new OnSuccessListener<Uri>() {
                                            @Override
                                            public void onSuccess(Uri uri) {
                                                String DocLink=uri.toString();
                                                linkdoc=DocLink;
                                                uploadData.setDocURL(linkdoc);
                                            }
                                        })
                                .addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                        Log.e("TAG_FOR_FAILURE LOG", "On Failure: The exception", e);
                                    }
                                });

                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                finished=false;
                                Toast.makeText(addEventActivity.this, "doc fucked", Toast.LENGTH_SHORT).show();
                            }
                        });
            }

            progressDialog.dismiss();


            String info=infoText.getText().toString();
            String event=eventName.getText().toString();

            uploadData.setName(event);
            uploadData.setInfo(info);

            //uploading the event name,info and other file links to the RTDB under the event name
            databaseReference.child(event).setValue(uploadData);

        }
    });

}

其中的按钮是updata, UploadData 是用于保存所有这些值的类...

Where updata is the button, UploadData is the class for holding those all those values...

单击按钮后,图像将存储在存储器中,而数据库则什么也没有,

After I click the button the image is stored in the Storage while the Database holds nothing but,

data {
   name:"given name"
   info:"given info"
}

,但必须包含 ImgLink DocLink . 它在哪里迷路了?

while it has to have included the ImgLink and DocLink. Where does it lose track?

推荐答案

对服务器的调用完成后,每个onSuccess方法均被异步调用.由于这需要时间,因此您的主要代码将继续,这意味着您将在下载URL可用之前写入数据库.

Each onSuccess method is called asynchronously, after the call to the server has completed. Since this takes time, you main code continues, and this means you're writing to the database, before the download URL is available.

如果在代码中放置一些日志语句,最容易看到这一点:

It's easiest to see this if you place some log statements in your code:

Log.i("STORAGE", "Starting to upload image data");
StorageReference storageReference1=storageReference.child(StoragePath + System.currentTimeMillis() + "." + getExtension(DocPathUri));

storageReference1.putFile(DocPathUri)
        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Log.i("STORAGE", "Image data uploaded, getting download URL");

                taskSnapshot.getStorage().getDownloadUrl()
                        .addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {
                                Log.i("STORAGE", "Got download URL");
                                String DocLink=uri.toString();
                                linkdoc=DocLink;
                                uploadData.setDocURL(linkdoc);
                            }
                        })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.e("TAG_FOR_FAILURE LOG", "On Failure: The exception", e);
                    }
                });

            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                finished=false;
                Toast.makeText(addEventActivity.this, "doc fucked", Toast.LENGTH_SHORT).show();
            }
        });

...

uploadData.setName(event);
uploadData.setInfo(info);

Log.i("STORAGE", "Writing info to database");
databaseReference.child(event).setValue(uploadData);

此输出为:

开始上传图像数据

Starting to upload image data

将信息写入数据库

上载图像数据,获得下载URL

Image data uploaded, getting download URL

获得下载网址

这可能不是您所期望的,但它按预期工作,并且完全解释了为什么未将下载URL写入数据库:到您写入数据库时​​,尚未加载下载URL

This is probably not what you expected, but it is working as intended, and completely explains why the download URL isn't written to the database: by the time you write to the database, the download URL hasn't been loaded yet.

此问题的解决方案始终相同:任何需要下载URL的代码都必须位于检索下载URL时调用的onSuccess方法内部(或至少需要从此处调用)

The solution to this problem is always the same: any code that requires the download URL, need to be inside the onSuccess method that gets called when the download URL is retrieved (or at least needs to be called from there).

所以像这样:

updata.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //before uploading both the doc and img..
            progressDialog.setTitle("Uploading Images");
            progressDialog.show();

            final UploadData uploadData=new UploadData();

            if(ImgPathUri!=null){
                StorageReference str=storageReference.child(StoragePath + System.currentTimeMillis() + "." + getExtension(ImgPathUri));

                str.putFile(ImgPathUri)
                        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

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

                                                String ImgLink=uri.toString();
                                                linkimg=ImgLink;
                                                uploadData.setImgURL(linkimg);

                                            }
                                        });

                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Toast.makeText(addEventActivity.this, "fucked ra", Toast.LENGTH_SHORT).show();
                                finished=false;
                            }
                        })
                        .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

                            }
                        });
            }

            if(DocPathUri!=null){
                StorageReference storageReference1=storageReference.child(StoragePath + System.currentTimeMillis() + "." + getExtension(DocPathUri));

                storageReference1.putFile(DocPathUri)
                        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                                taskSnapshot.getStorage().getDownloadUrl()
                                        .addOnSuccessListener(new OnSuccessListener<Uri>() {
                                            @Override
                                            public void onSuccess(Uri uri) {
                                                String DocLink=uri.toString();
                                                linkdoc=DocLink;

                                                uploadData.setDocURL(linkdoc);


                                                progressDialog.dismiss();


                                                String info=infoText.getText().toString();
                                                String event=eventName.getText().toString();

                                                uploadData.setName(event);
                                                uploadData.setInfo(info);

                                                //uploading the event name,info and other file links to the RTDB under the event name
                                                databaseReference.child(event).setValue(uploadData);
                                            }
                                        })
                                .addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                        Log.e("TAG_FOR_FAILURE LOG", "On Failure: The exception", e);
                                    }
                                });

                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                finished=false;
                                Toast.makeText(addEventActivity.this, "doc fucked", Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        }
    });

}

这篇关于Firebase存储-未创建链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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