简体   繁体   中英

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.

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

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).

Now you are sure that your method will be executed right after you get your 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();
                }

            }
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM