【发布时间】:2018-06-14 19:48:50
【问题描述】:
我正在开发一个应用程序,该应用程序假设将图像从 android 手机传输到服务器 - 客户端架构中的 PC(下面添加了 java (android) 代码)。照片假设通过 bytearray。我很难弄清楚如何通过 TCP 创建一个协议,该协议能够在不丢失信息的情况下传递我的图像,并且还能够传递图像的元数据,例如图像名称、扩展名、大小,也许是元数据大小(如果需要?)。我真的很感激你 帮助,因为我是 C# 的新手,并且正在编写客户端服务器,尤其是假设传输某种扩展图像的客户端。
private void makeTCPConnection() {
try {
InetAddress serverAddr = InetAddress.getByName("10.0.2.2");
//create a socket to make the connection with the server
Socket socket = new Socket(serverAddr, 8000);
try {
//Sends the message to the server
OutputStream output = socket.getOutputStream();
File dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
if(dcim == null)
return;
File[] pics=dcim.listFiles();
int count=0;
if(pics != null){
for(File pic:pics){
FileInputStream fis = new FileInputStream(pic);
Bitmap bm = BitmapFactory.decodeStream(fis);
byte[] imgbyte = getBytesFromBitmap(bm);
output.write(imgbyte);
output.flush();
}
}
}catch (Exception e){
Log.e("TCP","S:Error",e);
}finally {
socket.close();
}
}catch (Exception e){
Log.e("TCP","C:Error",e);
}
}
public byte[] getBytesFromBitmap(Bitmap bitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,70,stream);
return stream.toByteArray();
}
【问题讨论】:
-
您是使用安卓作为客户端,PC 作为服务器。 Socket程序将图像从手机传输到PC?
-
是的。我已经添加了代码。
标签: java c# android image client-server