dankito / RichTextEditor

Rich text WYSIWYG editor for Android and JavaFX
Apache License 2.0
95 stars 36 forks source link

how do i save the image in my server using volley #19

Closed kunz398 closed 5 years ago

kunz398 commented 5 years ago

i have this code at the moment

Button btn = (Button)rootView.findViewById(R.id.savecontnet);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            save();
        }
    });

save function

   private void save() {
        editor.getCurrentHtmlAsync(new GetCurrentHtmlCallback() {

            @Override
            public void htmlRetrieved(@NotNull String html) {
                savetoserver(html);
                //saveHtml(html);
            }
        });
    }

savetoserver function

 private void savetoserver(final String test) {

        String tag_string_req = "test";
//url for the php script to save to server 
        StringRequest strReq = new StringRequest(Request.Method.POST,
                AppConfig.URL_TEST, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {

            @Override
            protected Map<String, String> getParams() {

                Map<String, String> params = new HashMap<String, String>();
                params.put("content", test);
                return params;
            }
        };

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

now my php script is simple PHP SCRIPT

<?php
if ($_POST['content']){
    $content = $_POST['content'];

    $query = "INSERT INTO `testtable` (`dumplog`) VALUES ('$content')";
    if (mysqli_query($conn, $query))
    {
        $response["error"] = False;
        $response["success_msg"] = "Successful";
        echo json_encode($response);
    } else {
        $response["error"] = TRUE;
        $response["error_msg"] = "False";
        echo json_encode($response);
    }

}

so in my database it stores as a string i get that but what i want to do is if there is an image i want to download that to my server and then display it from there so the image source would be something like so

<img class="resizable" src="SERVER_URL/FOLDER_NAME/IMAGE_123.JPG" >
<br>
<p>Hello </p>

i think first i need to convert the image to string but before that i need to be able to get that image and save it as a varible in bitmap but i dont know how to go about this. i am fairly new at this concepts so a guide would be really helpful

dankito commented 5 years ago

Sorry, this is not related to RichTextEditor. I'm not responsible for implementing your application logic.

I make a suggestion how to do this here, but please don't ask me any further questions to this.

I personally would use Jsoup (org.jsoup:jsoup:1.11.2) to get all \ elements from html String, use their 'src' attribute to upload the image to your server and set the url of the now uploaded image in \ element:

// don't do this on UI thread, it's a long taking operation with network access which would block your application
private void uploadImagesAndSaveHtmlToServer(final String html) {
    Document doc = Jsoup.parse(html);
    Elements images = doc.select("img");

    for (Element imageElement : images) {
        String imageUrl = imageElement.attr("src");

        // uploads this image to your server and returns remote image url (= url of image on your server)
        String remoteImageUrl = uploadImageToServer(imageUrl);
        imageElement.attr("src", remoteImageUrl);
    }

    String htmlWithRemoteImageUrls = doc.outerHtml();
    savetoserver(htmlWithRemoteImageUrls); // calls your savetoserver(String) method
}