[英]Sending a bunch of images from an android phone to a PC
I'm working on an app that suppose to transfer images from an android phone to a PC in a server - client architecture (the java (android) code is added below).The photos suppose to pass through a bytearray. 我正在开发一个应用程序,该应用程序旨在将图像从Android手机传输到服务器中的PC-客户端体系结构(下面添加了Java(android)代码)。照片应该通过字节数组传递。 I'm having a hard time to figure out how to create a protocol over TCP that will be able to pass my images without loss of information and also be able to pass the META-DATA of the images such as the image name, extension, size, maybe META-DATA size (if needed?). 我很难弄清楚如何通过TCP创建一个协议,该协议将能够传递我的图像而不会丢失信息,还能够传递图像的META-DATA,例如图像名称,扩展名,大小,也许是META-DATA大小(如果需要?)。 I would really appreciate your help becuase I'm kind of new to C# and writing a client-server, especially one that suppose to transfer images of some sorts of extensions. 我非常感谢您的帮助,因为我是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();
}
Have you considered to write a small REST web service ? 您是否考虑过编写小型REST Web服务 ?
This approach would have some advantages: 这种方法将具有一些优点:
For Android I can recommend Retrofit as a REST client. 对于Android,我可以建议将Retrofit作为REST客户端。 And if you want to use Java on the server side, I would recommend Spring/Spring Boot . 而且,如果您想在服务器端使用Java,则建议使用Spring / Spring Boot 。 (How-to guide for a REST web service in Spring Boot: https://spring.io/guides/gs/rest-service/ ) (Spring Boot中的REST Web服务的操作指南: https : //spring.io/guides/gs/rest-service/ )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.