简体   繁体   English

如何将图像下载URL从FireBase存储发送到SQLServer数据库?

[英]How to send image download URL from FireBase Storage to SQLServer Database?

I'm having a problem in sending the download url to sql server database even though I get the url and set it to a TextView but it doesn't show up in the database. 我在将下载URL发送到sql server数据库时遇到问题,即使我获取了url并将其设置为TextView但它没有显示在数据库中。

Here's the code I've been trying, and tried many ways but still not working 这是我一直在尝试的代码,并尝试了很多方法,但仍然没有工作

public void Upload() {
    if (filePath != null) {

        final ProgressDialog progressDialog = new ProgressDialog(getActivity());
        progressDialog.setTitle("Uploading");
        progressDialog.show();

        final StorageReference riversRef = FirebaseStorage.getInstance().getReference().child("BrandImages/" + brandname.getText().toString() + ".jpg");

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

                progressDialog.dismiss();

                Task<Uri> task = taskSnapshot.getMetadata().getReference().getDownloadUrl();
                task.addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        String generatedFilePath = uri.toString();
                        imgpath.setText(generatedFilePath);
                    }
                });


                ConnectDatabase db = new ConnectDatabase();
                Connection con = db.ConnectDB();
                if (con == null)
                    Toast.makeText(getActivity(), "Please check your internet connection!", Toast.LENGTH_LONG).show();

                else {
                    try {
                        Statement sm = con.createStatement();
                        int x = sm.executeUpdate("insert into CarBrand values('" + brandname.getText() + "','" + imgpath.getText() + "')");

                        if (x == 0)
                            Toast.makeText(getActivity(), "an error occurred, please try again in a few moments.", Toast.LENGTH_LONG).show();
                        else {
                            Toast.makeText(getActivity(), "Brand has been added.", Toast.LENGTH_LONG).show();
                            brandname.getText().clear();
                             }
                    } catch (SQLException e) {
                        if (e.getErrorCode() == 2627)
                            Toast.makeText(getActivity(), "Brand already exists!", Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(getActivity(), "an error occurred", Toast.LENGTH_LONG).show();
                    }

                }


            }
        });

Now I get the url and set to (imgpath) which is a TextView but the problem is when it's inserted to the database it shows the default value of the TextView not the download URL 现在我得到了url并设置为(imgpath)这是一个TextView,但问题是当它插入到数据库时它显示TextView的默认值而不是下载URL

Try to put the method for you sql db inside the listener (I don't know the context of your code but this probably doesn't work cause firebase method is asynchronous). 尝试把你的方法为你的sql db在监听器内(我不知道你的代码的上下文,但这可能不起作用,因为firebase方法是异步的)。

Now you are sure that your method will be executed right after you get your URL. 现在您确定在获取URL后立即执行您的方法。

Like this : 像这样 :

Task<Uri> task = taskSnapshot.getMetadata().getReference().getDownloadUrl();
            task.addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    String generatedFilePath = uri.toString();
                    imgpath.setText(generatedFilePath);





            ConnectDatabase db = new ConnectDatabase();
            Connection con = db.ConnectDB();
            if (con == null)
                Toast.makeText(getActivity(), "Please check your internet connection!", Toast.LENGTH_LONG).show();

            else {
                try {
                    Statement sm = con.createStatement();
                    int x = sm.executeUpdate("insert into CarBrand values('" + brandname.getText() + "','" + imgpath.getText() + "')");

                    if (x == 0)
                        Toast.makeText(getActivity(), "an error occurred, please try again in a few moments.", Toast.LENGTH_LONG).show();
                    else {
                        Toast.makeText(getActivity(), "Brand has been added.", Toast.LENGTH_LONG).show();
                        brandname.getText().clear();
                         }
                } catch (SQLException e) {
                    if (e.getErrorCode() == 2627)
                        Toast.makeText(getActivity(), "Brand already exists!", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(getActivity(), "an error occurred", Toast.LENGTH_LONG).show();
                }

            }
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Firebase Storage检索图像的下载URL? - How to retrieve the download URL of an image from Firebase Storage? 如何从 Firebase Storage 获取下载网址? - How to get the download url from Firebase Storage? 如何从firebase存储中获取所有下载URL? - How to get all download url from firebase storage? 通过URL从Firebase存储下载视频 - Download video from Firebase storage by URL 如何使用 Firestore 中的图像 url 从 firebase 存储中获取图像 - How to get image from firebase storage with the image url that is in firestore 如何使用 Java 从 Android 中的 Firebase 存储获取图像 URL? - How to get image URL from Firebase Storage in Android using Java? 无法从 Firebase 存储中检索图像的下载 URL(出现异常)[需要紧急帮助] - Unable to retrieve image's download URL from Firebase Storage (getting exception) [NEED URGENT HELP] 如何将图像从存储链接到实时数据库(Firebase) - How to link image from storage to real time database (Firebase) 从 HTTPS 下载图像并上传到 Android Studio 中的 firebase 存储 - Download image from HTTPS and upload to firebase storage in Android Studio 在Firebase数据库的Imageview中显示存储在Firebase存储中的图像 - Show the image stored in Firebase storage in Imageview from Firebase Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM