我的应用将图像从服务器传输到客户端。现在的问题是,即使我选择了不同的图片,也只发送了一个(相同的)图像。我的服务器代码(我相信错误的地方)如下: N.B这些类在不同的文件中。
(String selectedImagePath)是我主要活动中的全局变量。
public void onSendClick(View v) {
serverSocketThread = new ServerSocketThread(SocketServerPORT, selectedImagePath);
serverSocketThread.start();
}
//*********************pick image**********************///////////////:
public void onBrowseClick(View v) {
if (checkPermissionREAD_EXTERNAL_STORAGE(this)) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
TextView path = (TextView) findViewById(R.id.tvPath);
path.setText("Image Path : " + selectedImagePath);
imageView.setImageURI(selectedImageUri);
//btnSend.setClickable(true);
}
}
}
public String getPath(Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
ServerSocketThread
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSocketThread extends Thread {
int SocketServerPORT;
ServerSocket serverSocket;
String selectedImagePath;
ServerSocketThread(int SocketServerPORT, String selectedImagePath) {
this.SocketServerPORT = SocketServerPORT;
this.selectedImagePath = selectedImagePath;
}
@Override
public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(SocketServerPORT);
while (true) {
socket = serverSocket.accept();
FileTxThread fileTxThread = new FileTxThread(socket, this.selectedImagePath);
fileTxThread.start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileTxThread
import android.content.Context;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class FileTxThread extends Thread {
Socket socket;
Context context;
String selectedImagePath;
FileTxThread(Socket socket, String selectedImagePath){
this.socket = socket;
this.selectedImagePath = selectedImagePath;
}
FileTxThread(Context context) {
this.context = context;
}
@Override
public void run() {
try {
/* if(imageView.getDrawable()!=null) {
bSend.setClickable(true);
}*/
File file = new File(this.selectedImagePath);
byte[] bytes = new byte[(int) file.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
bis.read(bytes, 0, bytes.length);
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(bytes);
oos.flush();
socket.close();
final String sentMsg = "File sent to: " + socket.getInetAddress();
((Selector)context).runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, sentMsg, Toast.LENGTH_LONG).show();
}});
Toast.makeText(context, sentMsg, Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这是一张似乎要发送的图片,请帮我弄清楚我哪里错了。我不确定问题是否与我的构造函数有关。