简体   繁体   中英

How to get size of the object in kilobyte(kb) or in byte

In my android application, it collect some data and photo throw mobile.

After that it insert all these things into an object and after that it send this object to the server.

Before sending this object onto server I want to show the size of the data to the user and how many killobyte used to send this data to the server. Here I am using progressBar which show only data is sending to the server.

Here what I want, before sending data to server first it show what is the size of data and after starting progressBar how may data transferred and how many renaming.

I am sending object like this:

public class ServerData implements Serializable {
// some code
}

One way to do that, is to write it to a buffer-stream, find its size then write it to your server as byte array.

       ByteArrayOutputStream ostream = new ByteArrayOutputStream ();
           try {
              ObjectOutputStream obStream = new ObjectOutputStream(ostream);
              obStream.writeObject(yourObject);
              byte[] rawObject = ostream.toByteArray();
              ostream.close();

              int size = rawObject.length;
             //edit for your case
             OutputStream socketOut = socket.getOutputStream();
             socketOut.write(rawObject);
             socketOut.close();

        } catch (IOException e) {
        e.printStackTrace();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM