2

In android studio I used this code to convert image to byte array and send to server:

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArrayImage = baos.toByteArray();
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT); 
ServerService.sendProfileImage(encodedImage);

In node-js back-end I use this code to write data to file:

let imageBuffer = new Buffer.from(data.info, 'Base64');
fs.writeFile(fd, imageBuffer, (err, written) => {
    if(err) {
        callBack(err);
        return;
    } else{
        fs.close(fd, () => {
            callBack(null);
            return;
        });
    }
})

Note: back-end works perfect with browser and saved images show correctly, > no problem; Android data is saved too, but image is not in correct format.

But some thing is wrong and Image is not a valid file.

Image upload well, reload from server well by browser.

Ahmad Ebrahimi
  • 267
  • 5
  • 24

1 Answers1

1

The java code produces correct Base64 image String.

There might be an issue while sending this string to your server. Try to print your base64 string on the server end to check whether you are getting full string from the request or not. if the string is correct then try following code to save the image

app.post('/save-image', (req, res) => {
    require("fs").writeFile("out.jpg", req.body.info, 'base64', function(err) {
        console.log(err);
    });
})

The above code uses body-parser package to access the info field.

This is my code which i've used in Android

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle

import android.util.Base64
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import okhttp3.*
import java.io.ByteArrayOutputStream
import java.io.IOException

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        submit.setOnClickListener {
            val bitmap = BitmapFactory.decodeResource(resources, R.drawable.out)
            val baos = ByteArrayOutputStream()
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos)
            val imageBytes: ByteArray = baos.toByteArray()
            val imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT)
            send(imageString)
        }


    }

    private fun send(imageString: String) {
        val body = FormBody.Builder()
        body.add("info",imageString)
        val request = Request.Builder().url("http://192.168.1.4:3000/save-image").post(body.build())
        OkHttpClient.Builder().build().newCall(request.build()).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                Log.e("Failure",Log.getStackTraceString(e))
            }

            override fun onResponse(call: Call, response: Response) {
                Log.e("response",response.body?.string() ?: "failed")
            }

        })
    }
}
Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89
  • Please pay attention: on server side it work with browser perfect, but what I receive from android is incompatible format. Your answer is far from understanding question, sorry. – Ahmad Ebrahimi May 16 '20 at 04:15
  • @AhmadEbrahimi I understand. I posted this answer after I made a test app where i used the above java code to convert image to base64 String, then I send it to nodejs server (using express & body-parser) being on my localhost and it worked perfectly. can you post the base64 string you received from your android application? – Sahil Manchanda May 16 '20 at 11:44
  • @AhmadEbrahimi I've updated my answer. for sending the data I've used OKHTTP – Sahil Manchanda May 16 '20 at 13:17
  • I will check the result, Thank you – Ahmad Ebrahimi May 16 '20 at 13:19
  • I did add my android code. please wait. let me zip my project and send it to you – Sahil Manchanda May 16 '20 at 13:21
  • @AhmadEbrahimi use this link to download android project: https://www.transfernow.net/3LMHoL052020 – Sahil Manchanda May 16 '20 at 13:24