英文:
Sending an image file from an Android phone to a PC through Python using sockets
问题
public void send(View v) {
Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(i, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(selectedImage);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] array = byteArrayOutputStream.toByteArray();
SendImageClient sendImageClient = new SendImageClient();
sendImageClient.execute(array);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "no image selected", Toast.LENGTH_SHORT).show();
}
}
public class SendImageClient extends AsyncTask<byte[], Void, Void> {
@Override
protected Void doInBackground(byte[]... voids) {
try {
Socket socket= new Socket("192.168.0.106",8888);
OutputStream out=socket.getOutputStream();
DataOutputStream dataOutputStream=new DataOutputStream(out);
dataOutputStream.writeInt(voids[0].length);
dataOutputStream.write(voids[0],0,voids[0].length);
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Image sent", Toast.LENGTH_SHORT).show();
}
});
dataOutputStream.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
英文:
I want to send an image from my android device to python through sockets, I've been successful in sending text from Android-Python.
When I send the image I get the toast of the image being sent but on the receiving end the image is corrupted with a 0 KB size
I appreciate any help, as I'm currently stuck here.
Here is my code
image.py
from socket import *
port = 8888
s = socket(AF_INET, SOCK_STREAM)
s.bind(('', port))
s.listen(1) #listens to 1 connection
conn, addr = s.accept()
print("Connected by the ",addr)
while True:
data = conn.recv(1024)
with open('image.jpg', 'wb') as file:
file.write(data)
conn.close()
The Android side code is,
public void send(View v) {
Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(i, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(selectedImage);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] array = byteArrayOutputStream.toByteArray();
SendImageClient sendImageClient = new SendImageClient();
sendImageClient.execute(array);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "no image selected", Toast.LENGTH_SHORT).show();
}
}
public class SendImageClient extends AsyncTask<byte[], Void, Void> {
@Override
protected Void doInBackground(byte[]... voids) {
try {
Socket socket= new Socket("192.168.0.106",8888);
OutputStream out=socket.getOutputStream();
DataOutputStream dataOutputStream=new DataOutputStream(out);
dataOutputStream.writeInt(voids[0].length);
dataOutputStream.write(voids[0],0,voids[0].length);
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Image sent", Toast.LENGTH_SHORT).show();
}
});
dataOutputStream.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
答案1
得分: 0
你的Python代码在每次循环运行时都会截断输出文件。在最后一次运行时,当连接关闭时,文件会被截断,conn.recv
将返回一个空字符串,并且不会将其他任何内容写入文件。
将 with open(..)
移出从套接字读取的循环,并处理连接关闭,以便避免无限循环:
with open('image.jpg', 'wb') as file:
while True:
data = conn.recv(1024)
if not data: break
file.write(data)
你的Java代码在发送实际数据之前会先发送数据的大小 - 你应该要么删除这部分,要么调整Python代码以考虑这一点。
dataOutputStream.writeInt(voids[0].length);
dataOutputStream.write(voids[0], 0, voids[0].length);
英文:
Your python code is truncating the output file on every run of the loop. On the last run, when the connection is closed, the file is truncated, conn.recv
will return an empty string, and nothing else will be written to the file.
Move with open(..)
outside the loop that reads from the socket, and handle the connection closing so you don't have an infinite loop:
with open('image.jpg', 'wb') as file:
while True:
data = conn.recv(1024)
if not data: break
file.write(data)
Your java code is sending the size of the data it's sending before the data itself - you should either remove this or adjust the python code to take this into account.
dataOutputStream.writeInt(voids[0].length);
dataOutputStream.write(voids[0],0,voids[0].length);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论