Stream file from android on server

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Stream file from android on server

Postby pitrsonek » Fri Jan 08, 2010 11:30 am

Hi, please help me with this?

I try develop for android client app, this use TCP and connect to server. I want try stream any file on the server, this file I need read to byte.

Can you show me how I can do it. Need do nee thread for this stream file?

Later a want record MIC input a stream realtime on server.

Client source:
----------------------------------

package androvoice.nti.tul.cz;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;

import android.util.Log;


public class TCPClient{
private PrintWriter out;
private Socket socket;
private OutputStream outputStream;
private int bufferByteSize = 1024;

public TCPClient(){
try {
InetAddress serverAddr = InetAddress.getByName("1.1.1.1");
//InetAddress serverAddr = InetAddress.getByName("127.0.0.1");
Log.d("TCP", "C: Connecting...");
try{
this.socket = new Socket(serverAddr, 4444);
}catch (Exception e2) {
Log.e("TCP", "C: Nepodarilo se vytvorit socket.", e2);
}
this.out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
this.outputStream = socket.getOutputStream();
}catch (Exception e) {
Log.e("TCP", "C: Nepodarilo se pripojit.", e);
}
}

public PrintWriter getOut(){
return this.out;
}

public void sendFile(InputStream is){
byte[] buffer = new byte[this.bufferByteSize];
int bytesRead = 0;
try {
while ((bytesRead = is.read(buffer)) != -1) {
try {
this.outputStream.write(buffer);
Log.d("TCP", "C: Zapisuji z bufferu do streamu");
} catch (IOException e) {
Log.d("TCP", "C: Nepodarilo se zapsat do streamu");
}
}
} catch (IOException e) {
Log.d("TCP", "C: nelze cist z bufferu");
}
}

public void SendMessage(String message){
try {
Log.d("TCP", "C: Sending: '" + message + "'");
this.out.println(message);
Log.d("TCP", "C: Sent.");
Log.d("TCP", "C: Done.");

} catch(Exception e) {
Log.e("TCP", "S: Error", e);
}
}

public void closeClient(){
try {
this.socket.close();
} catch (IOException e) {
Log.e("TCP", "S: Error close socked", e);
}
}
}


Source in Activity
----------------------------------------------

public void onClick(View v) {
if(v.getId()==R.id.buttonSendMessage){
String sMessage = this.tMessage.getText().toString();
//File file = new File("/sdcard/download/pricti.mpga");
try {
InputStream fIn = new FileInputStream("/sdcard/download/pricti.mpga");

this.client.sendFile(fIn);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
//InputStream fIn = InputStream(FileInputStream(file));
//InputStream fIn = openFileInput("/sdcard/download/pricti.mpga");

//this.client.sendFile(fIn);
this.client.SendMessage(sMessage);
}
if(v.getId()==R.id.buttonCloseServer){
this.client.closeClient();
}

}


THX I have not develop in JAVA.
pitrsonek
Freshman
Freshman
 
Posts: 3
Joined: Thu Oct 08, 2009 10:54 pm

Top

Postby Draffodx » Fri Jan 08, 2010 1:23 pm

what errors are you getting?
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Postby pitrsonek » Fri Jan 08, 2010 1:37 pm

Draffodx wrote:what errors are you getting?


Do you think this code is ok?
Maybe is bug in my server, then i code in C#..

THX
pitrsonek
Freshman
Freshman
 
Posts: 3
Joined: Thu Oct 08, 2009 10:54 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 2 guests