我想用PC将图像从我的PC发送到Android手机。传输图像文件(文件大小匹配)。但是,无法读取图像。 我在Shartphone的PC和服务器端代码中编写了客户端代码。
如果你能告诉我问题在哪里,我将不胜感激。提前谢谢。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
代码链接 https://drive.google.com/drive/folders/1MZz6FSWpFWgCyrvtTNXUakHTZll5n84n?usp=sharing
服务器端:智能手机接收图片
package com.example.an_server_img_receive;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runThread1();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
/////////////////////////////// run
/////////////////////////////// thread//////////////////////////////////////////////
private void runThread1() // to receive image from server/laptop
{
new Thread() {
public void run()
{
Log.d("p2p", "1");
try {
/////////////////////
// System.out.println("aaa");
ServerSocket serverSocket = new ServerSocket(6000);
Socket clientSocket = serverSocket.accept();
String format = new SimpleDateFormat("yyyyMMddHHmmss", java.util.Locale.getDefault())
.format(new Date());
Log.d("p2p", "2");
Log.d("p2p", "2a"+ clientSocket.isConnected());
DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
Log.d("p2p", "2b"+ clientSocket.getKeepAlive());
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
+ "/Camera/" + File.separator + format + ".jpg");
Log.d("p2p", "#" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));
Log.d("p2p", "3");
f.createNewFile();
DataOutputStream dos = new DataOutputStream(new FileOutputStream(f));
Log.d("p2p", "4");
byte[] buffer = new byte[1024];
int len;
while ((len = dis.read(buffer)) != -1)
{
dos.write(buffer, 0, len);
}
Log.d("p2p", "5");
dis.close();
dos.close();
serverSocket.close();
Log.d("p2p", "6");
runOnUiThread(new Runnable()
{
@Override
public void run()
{
//tv1.setText("#" + i);
Toast.makeText(getApplicationContext(), "Image is received", Toast.LENGTH_LONG).show();
}
});
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}
PC:发送图片
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
/* CLIENT SEND AN IMAGE */
public class myclienttcpimage
{
public static void main(String args[])
{
try
{
Socket socket = new Socket("192.168.0.101", 6000);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
//out.writeChar('I'); // as image,
DataInputStream dis = new DataInputStream(new FileInputStream("D:\\image.jpg"));
ByteArrayOutputStream ao = new ByteArrayOutputStream();
int read = 0;
byte[] buf = new byte[1024];
long start = System.nanoTime();
while ((read = dis.read(buf)) > -1)
{
ao.write(buf, 0, read);
}
long end = System.nanoTime();
long time = end-start;
System.out.println("time " + time);
System.out.println("c:" + ao.size());
out.writeLong(ao.size());
out.write(ao.toByteArray());
out.flush();
out.close();
dis.close();
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}