Note that there are some explanatory texts on larger screens.

plurals
  1. POFILE TRANSFER in android
    primarykey
    data
    text
    <p>this is regarding android instant messenger. im trying to send a file, and this is how i send it :</p> <pre><code>@Override public boolean sendFile(String path,String ip, int port) { // TODO Auto-generated method stub try { String[] str = ip.split("\\."); byte[] IP = new byte[str.length]; for (int i = 0; i &lt; str.length; i++) { IP[i] = (byte) Integer.parseInt(str[i]); } Socket socket = getSocket(InetAddress.getByAddress(IP), port); if (socket == null) { Log.i("SO sendFILE","null"); return false; } Log.i("SocketOP", "sendFILE-1"); File f = new File(path); BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() ); FileInputStream fileIn = new FileInputStream(f); Log.i("SocketOP", "sendFILE-2"); byte [] buffer = new byte [1024]; int bytesRead =0; while ((bytesRead = fileIn.read(buffer)) &gt; 0) { out.write(buffer, 0, bytesRead); System.out.println("SO sendFile" + bytesRead); } out.flush(); out.close(); fileIn.close(); Log.i("SocketOP", "sendFILE-3"); } catch (IOException e) { return false; //e.printStackTrace(); } // Toast.makeText(this, "Lvbvhhging...", Toast.LENGTH_SHORT).show(); return true; } </code></pre> <p>this is how i receive connection and seperate text from file (i concatenate "text" to the output stream for the text) </p> <pre><code>public ReceiveConnection(Socket socket) { this.clientSocket = socket; this.fileSocket=socket; SocketOperator.this.sockets.put(socket.getInetAddress(), socket); } @Override public void run() { try { // PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); // PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); InputStream is=clientSocket.getInputStream(); String inputLine; while ((inputLine = in.readLine()) != null) { if (inputLine.contains("Text") == true) { appManager.messageReceived(inputLine); Log.i("SocketOP","text");} else if (inputLine.contains("Text") == false) { Log.i("SocketOP","filee"); appManager.fileReceived(is); } else{ clientSocket.shutdownInput(); clientSocket.shutdownOutput(); clientSocket.close(); fileSocket.shutdownInput(); fileSocket.shutdownOutput(); fileSocket.close(); SocketOperator.this.sockets.remove(clientSocket.getInetAddress()); SocketOperator.this.sockets.remove(fileSocket.getInetAddress()); Log.i("SocketOP", "CLOSING CONNECTION"); } } } catch (IOException e) { Log.e("ReceiveConnection.run: when receiving connection ",""); } } } </code></pre> <p>and this is how i finally receive the file in a service called imService :</p> <pre><code>public void fileReceived(InputStream is) throws FileNotFoundException, IOException { Log.i("IMSERVICE", "FILERECCC-1"); if (is!= null) { FileOutputStream fos = null; BufferedOutputStream bos = null; try { fos = new FileOutputStream("/sdcard/chats/ffffff.txt"); bos = new BufferedOutputStream(fos); byte[] aByte = new byte[1024]; int bytesRead = 0; System.out.println("imService fileReceive" + bytesRead); while ((bytesRead = is.read(aByte)) != -1) { bos.write(aByte, 0, bytesRead); System.out.println("imService fileReceive" + bytesRead); } bos.flush(); bos.close(); Log.i("IMSERVICE", "FILERECCC-2"); } catch (IOException ex) { // Do exception handling } } </code></pre> <p>right where i receive the file connection and forward the inputstream IS to the file receive method, i see that its getting the bytes but once filereceived is called it isnt receving any bytes.</p> <p>it uses tcp/ip.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload