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

时间:2019-04-18 12:47:18

标签: java android sql-server

即使我将URL下载并将其设置为TextView,但在将下载URL发送到sql服务器数据库时遇到问题。

这是我一直在尝试的代码,尝试了很多方法但仍然无法正常工作

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();
                    }

                }


            }
        });

现在,我获得了URL并将其设置为(imgpath)这是一个TextView,但是问题是,当它插入数据库时​​,它显示了TextView的默认值而不是下载URL

1 个答案:

答案 0 :(得分:0)

尝试将用于sql db的方法放在侦听器中(我不知道您的代码的上下文,但这可能不起作用,因为firebase方法是异步的。)

现在,您确定在获取URL后将立即执行您的方法。

赞:

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();
                }

            }
                }
            });