0

I am trying send a image between two device with socketio.I can send pictures, but it takes too long.I use byte array for image transfer.

 public static byte[] getBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}

// convert from byte array to bitmap
public static Bitmap getImage(byte[] image) {
    return BitmapFactory.decodeByteArray(image, 0, image.length);
}

I use getImage(return bitmap) for showing.And I use getBytes(from bitmap) for image transfer.
Server log (image byte array) stream of byte array takes too long
enter image description here


so that the transfer process takes too long.Is there a faster solution you know? I use gottox-client on android for socketio.Thanks in advance..
UPDATE
in android:

            Message msg = new Message();
            msg.setType("IMAGE");
            msg.setImage(DbBitmapUtility.getBytes(bmp));
            msg.setSender(userName);
            msg.setTo(toName);
            Gson gson = new Gson();
            String json = gson.toJson(msg);

            socket.emit("message", json);

in nodejs server:

                socket.on('message', function(message) {
                   users[obj.to].emit('event', message);
                   //save to mongodb on nodejs
               }

on receive in android:

            if (event.toString().equals("event")) {

            Type listType = new TypeToken<Message>() {
            }.getType();

            Gson gson = new Gson();
            Message message = gson.fromJson(args[0].toString(), listType);
            //conditions
            mMessages.add(message);
            scrollMyListViewToBottom();//notifydatasetChanged
        }
erginduran
  • 1,678
  • 5
  • 28
  • 51
  • You should show your send and receive code if you tell that the transfer is slow. – greenapps Mar 20 '15 at 20:48
  • Have you tried CompressFormat.JPG ? And did you check if indeed emit() is the culprit? – greenapps Mar 21 '15 at 08:28
  • `getBytes(Bitmap bitmap)` Where do you get the Bitmap from? From file? – greenapps Mar 21 '15 at 08:30
  • selected/picked image from gallery in android device – erginduran Mar 21 '15 at 11:12
  • I tried JPG compression.but problem continues – erginduran Mar 21 '15 at 11:15
  • The user picked an image from gallery. Ok. But please tell also how you got the Bitmap. And you did not tell if emit() caused the long time. Please measure. Please tell how many bytes you send and how long it takes. – greenapps Mar 21 '15 at 11:17
  • Sorry.OK.I get bitmap like this: bmp = BitmapFactory.decodeFile(filePath);..I get selected image path with this codes(getPath) http://stackoverflow.com/questions/20067508/get-real-path-from-uri-android-kitkat-new-storage-access-framework/20559175#20559175 – erginduran Mar 21 '15 at 11:21
  • Are you sure the long time is in sending the data? Not in all this bitmap manupilating and json encoding? Please measure with System.currentMillis ( or something like that). – greenapps Mar 21 '15 at 11:23
  • i do not know where it.But I am manually checking in server console.Transferring the number(byte array) appearing on the above picture takes too long.I can see that.And I can not send more data during the transfer with same device(uploader device). – erginduran Mar 21 '15 at 11:29
  • I'll try sending as a string(base64.encodetoString).Maybe string goes faster than byte array.Thanks a lot. – erginduran Mar 21 '15 at 11:33
  • Do not display that byte array in that way as THAT will cost a lot of time. Do not send base64 as you have to send 30% bytes more than now. Why cannot you measure time in android? Do not log every received byte on the server as THAT will take time. – greenapps Mar 21 '15 at 11:36

0 Answers0