2016-11-15 1 views
0

Comment je peux créer cadre qui ressemble à ceci:Java android envoyer une photo au serveur, comment puis-je créer un cadre?

0xB (byte) 
0xA (byte) 
0xA (byte) 
0xD (byte) 
width img (short) 
height picture (short) 
size img in byte (int) 
Binary data PICTURE 
0xB (byte) 
0xE (byte) 
0xE (byte) 
0xF (byte) 

Je ne sais pas comment je peux créer ce cadre.

Une largeur et la hauteur img je reçois comme ceci:

BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(R.drawable.icon); 
int height=bd.getBitmap().getHeight(); 
int width=bd.getBitmap().getWidth(); 

Ici, je reçois la taille:

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
    R.drawable.ic_launcher); 
Bitmap bitmap = bitmapOrg; 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
byte[] imageInByte = stream.toByteArray(); 
long lengthbmp = imageInByte.length; 

Répondre

0

Semble Vous avez besoin Socketconnection. Quelque chose comme ça (après votre code):

Socket clientSocket; 

clientSocket = new Socket(<address_of_server>, <number_of_port>); 

DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream()); 
dos.writeByte((byte)0xB); 
dos.writeByte((byte)0xA); 
dos.writeByte((byte)0xA); 
dos.writeByte((byte)0xD); 
dos.writeShort(width); 
dos.writeShort(height); 
dos.writeInt(lengthbmp); 
dos.write(imageInByte); 
dos.writeByte((byte)0xB); 
dos.writeByte((byte)0xE); 
dos.writeByte((byte)0xE); 
dos.writeByte((byte)0xF); 
... 
clientSocket.close(); 

Vous pouvez google plus d'exemples de connexion Socket, par exemple this.

Mise à jour

Vous pouvez également utiliser des prises pour envoyer des chaînes à votre serveur:

clientSocket = new Socket(serverAddr, SOCKET_SERVERPORT); 

dos = new DataOutputStream(clientSocket.getOutputStream()); 
dos.writeUTF("Hello"); 
dos.writeUTF("World!"); 
... 

Et vous devriez le faire dans le thread séparé (non IU). Exemple complet est quelque chose comme ça:

class SocketClientThread implements Runnable { 
     DataInputStream dis; 
     DataOutputStream dos; 
     String strResponseData; 

     @Override 
     public void run() { 
      try { 
       InetAddress serverAddr = InetAddress.getByName("<address>"); 
       clientSocket = new Socket(serverAddr, <port_number>); 
       dos = new DataOutputStream(clientSocket.getOutputStream()); 
       dis = new DataInputStream(clientSocket.getInputStream()); 

       // now you can write data to stream 
       dos.writeUTF("Hello"); 
       dos.writeUTF("World!"); 

       // you can also read data from stream 
       strResponseData = dis.readUTF(); 


      } catch (UnknownHostException ignore) { 
      } catch (IOException ignore) { 
      } 

      finally{ 
       if (clientSocket != null){ 
        try { 
         clientSocket.close(); 
        } 
        catch (IOException ignore) { 
        } 
       } 
      } 
     } 
} 

Et que vous pouvez utiliser SocketClientThread cette façon:

Thread socketClientThread; 
socketClientThread = new Thread(new SocketClientThread()); 
socketClientThread.start(); 
+0

Andriy Omelchenko pourriez-vous me exmplain comment je peux envoyer un socket à l'aide? –

+0

Tout d'abord, votre serveur devrait pouvoir traiter la connexion 'socket'. Dans ce cas, vous pouvez créer un flux de téléchargement et y écrire des octets en fonction de votre format de données. [Ici] (http://android-er.blogspot.com/2014/02/android-sercerclient-example-client.html) est un autre bon exemple. –

+0

Ok mais par exemple je veux envoyer une chaîne "Hello world" en octet et je ne sais pas comment je peux modyf cet exemple pour le faire. Pourriez-vous m'aider ? –