0

I am a beginner in socket.io. I have been used a library

https://www.npmjs.com/package/socket.io-stream

We were successfully uploaded images using the browser. But now, I want to upload images from android application. If anyone have android code please give me ..

https://github.com/socketio/socket.io-client-java/issues/29

I have been searching on google, but not found any proper solution.

3 Answers 3

3
 var imageBuffer = customJs.decodeBase64Image(base64Data); var imageTypeDetected = imageBuffer.type.match(/\/(.*?)$/); var filename = 'profile-' + Date.now() + '.' + imageTypeDetected[1]; // config.uploadImage --- Folder path where you want to save. var uploadedImagePath = config.uploadImage + filename; try { fs.writeFile(uploadedImagePath, imageBuffer.data, function () { dbMongo.updateImage({email: decoded.email, user_id: decoded.userId, 'profile_picture': config.showImagePath + filename}, function (res) { if (res.error) { socket.emit('set_update_image', {'error': 1, 'message': 'Error!' + res.message, 'data': null, 'status': 400}); } else { console.log(res); socket.emit('set_update_image', res); } }); }); } catch (e) { socket.emit('set_update_image', {'error': 1, 'message': 'Internal server error ' + e, 'data': null, 'status': 400}); } 

From other file call a function

exports.decodeBase64Image = function decodeBase64Image(dataString) { var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/); var response = {}; if (matches.length !== 3) { return new Error('Invalid input string'); } response.type = matches[1]; response.data = new Buffer(matches[2], 'base64'); return response; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks yogesh. working good. But base64 is not upload 5mb and above file. Have any ideas.
0

For the upload image from android using socket you need to send image as base64 string,

Following is the example for convert Image into base64 then you send data same as another paramaters.

String base64Image = getBase64Data(dirPath + "/" + fileName); public String getBase64Data(String filePath) { try { InputStream inputStream = new FileInputStream(filePath);//You can get an inputStream using any IO API byte[] bytes; byte[] buffer = new byte[8192]; int bytesRead; ByteArrayOutputStream output = new ByteArrayOutputStream(); try { while ((bytesRead = inputStream.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } } catch (Exception e) { e.printStackTrace(); } bytes = output.toByteArray(); return "data:image/jpeg;base64," + Base64.encodeToString(bytes, Base64.DEFAULT); } catch (Exception e) { e.printStackTrace(); } return ""; } 

2 Comments

please socket-io-stream library this is npm package. Is your code work with this library
I have not done any code related socket I suggested you here, You can send image data just converting in string.
0

In android, you need to encode the image by using Base64

 public void sendImage(String path) { JSONObject sendData = new JSONObject(); try{ sendData.put("imageData", encodeImage(path)); socket.emit("image",sendData); }catch(JSONException e){ } } private String encodeImage(String path) { File imagefile = new File(path); FileInputStream fis = null; try{ fis = new FileInputStream(imagefile); }catch(FileNotFoundException e){ e.printStackTrace(); } Bitmap bm = BitmapFactory.decodeStream(fis); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG,100,baos); byte[] b = baos.toByteArray(); String encImage = Base64.encodeToString(b, Base64.DEFAULT); //Base64.de return encImage; } 

In server side, receive image and decode it

socket.on("image", function(info) { var img = new Image(); img.src = 'data:image/jpeg;base64,' + info.imageData; }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.