我的客户希望仅使用 LAN 连接将视频文件从他的相机存储传输到他的 Android 手机。这可能吗?目前,我唯一能做的就是从手机存储中播放视频并流式传输 HTTP 或 RTSP 流视频,但是可以在通过 LAN 发送视频文件的同时流式传输吗?
public class Client extends Activity {
private Socket client;
private FileInputStream fileInputStream;
private BufferedInputStream bufferedInputStream;
private OutputStream outputStream;
private Button button;
private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1);
text = (TextView) findViewById(R.id.textView1);
//Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
File file = new File("/storage/emulated/BaseAhri.jpg");
try {
client = new Socket("10.0.2.2", 4444);
byte[] mybytearray = new byte[(int) file.length()];
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(mybytearray, 0, mybytearray.length);
outputStream = client.getOutputStream();
outputStream.write(mybytearray, 0, mybytearray.length);
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
client.close();
text.setText("File Sent");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
服务器端
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStream inputStream;
private static FileOutputStream fileOutputStream;
private static BufferedOutputStream bufferedOutputStream;
private static int filesize = 10000000;
private static int bytesRead;
private static int current = 0;
public static void main(String[] args) throws IOException {
serverSocket = new ServerSocket(4444);
System.out.println("Server started. Listening to the port 4444");
clientSocket = serverSocket.accept();
byte[] mybytearray = new byte[filesize];
inputStream = clientSocket.getInputStream();
fileOutputStream = new FileOutputStream("/sdcard/DCIM/Camera/BaseAhri.jpg");
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);});
System.out.println("Receiving...");
bytesRead = inputStream.read(mybytearray, 0, mybytearray.length);
current = bytesRead;
do {
bytesRead = inputStream.read(mybytearray, current, (mybytearray.length - current));
if (bytesRead >= 0) {
current += bytesRead;
}
} while (bytesRead > -1);
bufferedOutputStream.write(mybytearray, 0, current);
bufferedOutputStream.flush();
bufferedOutputStream.close();
inputStream.close();
clientSocket.close();
serverSocket.close();
System.out.println("Sever recieved the file");
}
服务器端错误
[2014-01-22 15:20:15 - AndroidSocketSERVER] ActivityManager: 启动: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.javacodegeeks.android.androidsocketserver /.服务器}
[2014-01-22 15:20:15 - AndroidSocketSERVER] ActivityManager:错误类型 3
[2014-01-22 15:20:15 - AndroidSocketSERVER] ActivityManager:错误:活动类 {com.javacodegeeks.android.androidsocketserver/com.javacodegeeks.android.androidsocketserver.Server} 不存在。
在客户端发送后它崩溃了。不确定这是否是一个错误,因为我的服务器有问题。
确实如此。您需要在应用程序中构建自己的套接字服务器并从中播放视频。然后您将可以控制字节输入流并将下载的部分保存到文件,而相同的部分将转到媒体播放器