Sending Image by file in Android (with FileInputStream) to PHP and saving mysql by Blob

how to retrieve blob data from mysql database using java
how to store image in mysql database using java
how to retrieve blob image from mysql database in android
image to blob in java
how to store pdf file in mysql database using java
jdbc blob
how to insert image in database using jdbc
how to store image in database using netbeans
I'm trying to send an image converted by file and fileinputstream with my android app for my PHP server and then save it in BLOB field in MySQL DB, but I tried too many things, but nothing is ok. What can I do? And so, is this possible? Help, please!

My PHP server receives the json and saves other fields in like normal, but the image doesn't save in blob field as I do with java NetBeans.

public void cadastrarTAG(ModeloTAG tag) throws JSONException {

    FileWriter writeFile = null;

    JSONObject json = new JSONObject();

    json.put("tag", String.valueOf(tag.getTag()));
    json.put("equipamento", String.valueOf(tag.getEquipamento()));
    json.put("data_registro", String.valueOf(tag.getData_registro()));
    json.put("login", String.valueOf(tag.getLogin()));
    json.put("descricao", String.valueOf(tag.getDescricao()));
    json.put("obs", String.valueOf(tag.getObs()));
    json.put("total_manutencoes", 0);
    json.put("setor", String.valueOf(tag.getSetor()));
    json.put("imagemFileLength", tag.getFile().length()); Field file length
    json.put("imagemFIS", tag.getFis()); field fileinputstream


    try {
        writeFile = new FileWriter(new File(android.os.Environment.getExternalStorageDirectory(), "saida.json"));
        writeFile.write((json.toString()));
        writeFile.flush();
        writeFile.close();

    } catch(Exception e){
        System.err.println("ERRO-> "+e);
        //e.printStackTrace();
    }

    try {
        log.geraLog("Cadastro de TAG ("+tag.getSetor()+") (MOBILE)", "PINS", (String) Login.rotinas[1], md.getDataHora());
        enviaJson.enviaJsonGravar(arquivoPHP, json);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

My PHP server receives the json and saves other fields in like normal, but the image doesn't save in blob field as I do with java NetBeans.

public function salvaImagem($conn, $json, $cod) {
          $flag['code'] = 0;
            echo "imagemFileLength 3 ---->>>>> ".$json->{'imagemFileLength'};
            echo "imagemFIS 3 ---->>>>> ".$json->{'imagemFIS'};

            $stmt = $conn->prepare("update tag set imagem = ? where id = ".$cod." and login = '{$json->{'login'}}'");
            $imageContent = fread($json->{'imagemFIS'}, filesize($json->{'imagemFileLength'}));
            //$imageContent = mysqli_real_escape_string($conn, $imageContent);
            //$stmt->bind_param('s',  mysql_real_escape_string($conn, $json->{'imagemFIS'}));
            $stmt->bind_param('s',  $imageContent);
            //$stmt->bindValue(1, $json->{'imagemFIS'});
            $stmt->execute();

            if ($stmt->execute()) {
              echo "New record created successfully";
            } else {
              echo "Unable to create record";


            $stmt->close();
            $conn->close();
        }

In this tutorial, we will show you how to write and read MySQL BLOB data using Next, read data from the sample resume file using FileInputStream and call  Contents in this project Android Upload Image To Server Using PHP MySQL : Watch the live demo. Create database along with table on your server to store image information. Create folder on your server to save images. Create PHP script to receive image send by android app. Start a fresh android application development project.

I suppose you're trying to put the image in json by calling json.put("imagemFIS", tag.getFis());.

It just writes the FileInputStream object to json. You should probably try and read image to byte[], convert it to a Base64-encoded String and then put it in your json.

Also, it might be better to keep image file's path or URI as a String in your ModeloTAG object rather then FileInputStream, i.e.:

public void cadastrarTAG(ModeloTAG tag) throws JSONException {
    ...
    /* read bytes */
    try {
        final byte[] fileBytes = getFileBytes(new File(tag.getPath));
        json.put("image", Base64.encodeToString(fileBytes, Base64.DEFAULT));
    } catch (FileNotFoundException e) {
        // Handle the exception
    } catch (IOException e) {
        // Handle the exception
    }
    ...
}

private byte[] getFileBytes(final String path) throw IOException, FileNotFoundException {
    final FileInputStream fis = new FileInputStream(new File(path));
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();

    final byte[] buffer = new byte[2048];

    int read = 0;

    while ((read = fis.read(buffer, 0, buffer.length)) > 0) {
        bos.write(buffer, 0, read);
    }

    fis.close();

    return bos.toByteArray();
}

And on your backend you just need to decode the Base64-encoded string and save it as blob or just as is.

Hope that helps!

I'm trying to send an image converted by file and fileinputstream with my android app for my PHP server and then save it in BLOB field in MySQL DB, but I tried  config.php– This file will have Database Name, Username and Password. upload-image-to-server.php– This file receive image send form android application and store in server. Open htdocs folder under C:\xampp and Create folder Android Upload Image .Under this folder create one upload folder. This upload folder will store all image send by

The problem is solved, I fix by sending imageview converted in Base64 for my PHP server and converting it into blob with this method.

public function salvaImagem($conn, $json, $cod) {
          $flag['code'] = 0;
            $blobData = base64_decode($json->{'imagem64'}); /* BASE64_DECODE and saving it like String with bind_param */
            $stmt = $conn->prepare("update tag set imagem = ? where id = ".$cod." and login = '{$json->{'login'}}'");
            $stmt->bind_param('s',  $blobData);
            $stmt->execute();

            if ($stmt->execute()) {
              echo "New record created successfully";
            } else {
              echo "Unable to create record";


            $stmt->close();
            $conn->close();
        }

Here you can learn C, C++, Java, Python, Android Development, PHP, SQL, JavaScript, But we can store images directly in database using BLOB (Binary Large file=new File("E:\\image.png"); FileInputStream fis=new FileInputStream(​file);  Hello friends welcome to our new Android Upload Image Using PHP MySQL Tutorial. I already published tutorials about how to upload image to server from android. But in last tutorial I stored android images to MySQL database. Now in this tutorial we won’t store image to MySQL database instead we will save our image inside a directory.

Whenever you need to send very large binary value you can use this If you need to store an image in a database using the JDBC program create a table with a Blob datatype as shown below: Following is an example demonstrating how to insert image in to a MySQL database using JDBC program. If you liked the demonstration of Android Upload Image to Server App then go ahead to know how you can do this. Lets start creating our Android Upload Image to Server Project. This Android Upload Image to Server is having two parts. PHP MySQL; Android Studio; Lets start with PHP MySQL for your Android Upload Image to Server app.

Here, we will upload and store the image file to our server's directory, and the folder create a file named api.php and write the following php code. int bytesRead = fileInputStream.read(buffer, 0, bufferSize); and we will send this array to the server Android Upload Image to Server Using PHP MySQL. In this Android Upload File to Server tutorial, we are going to discuss about how to select a file in android and upload it to the server using New Android Studio. Using the following code, we can successfully upload images, videos, documents, zip files, etc.. to the server. The code is tested for files upto 10MB of size.

In this example we're going to make use of MySQL and they have BLOB support. This Duration: 7:40 Posted: Sep 11, 2014 Blob: JDBC deals with Binary Data: 7. Blob and JDBC: Image: 8. Blob: Image 2: 9. Blob: image 3: 10. Insert an Image: 11. Retrieve an Image: 12. Store and retrieve an object from a table: 13. Read CLOBs data from database: 14. Getting BLOB Data from a Database Table: how to retrieves bytes from a BLOB. 15. Store CLOBs data into database? 16.

Comments
  • I already know how to save an image with JAVA front/backend. But my backend is PHP (in remote server and DB), so I´m trying to send my fileinputstream to PHP and use php to save in DB blob field
  • I understand that, but FileInputStream only contains the FileDescriptor which won't be of much use on your backend (apart from that you will have to somehow deserialize it), that's why you need to get an, i.e. Base64-encoded String from it. On your backend you can use base64_decode to decode string to the array of bytes which you can save to the DB.
  • So your hint is send image converted into base64 to my php backend, convert the base64 to something like I can save into my db blob? is this?
  • @Kylukz yes, you're right. Or you can also try saving the image decoded from base64 to a file and keep it's path or id in the database.
  • I prefer to convert image base64 sent by my android app to my php server, coding this image and save like blob, because my desktop software already created like this; saving by blob