I am a beginner in android development i have a question on how can i modify my code to send multiple files over socket, With my current code i can only send one file at a time, I have an ArrayList which is obtained from other Activity which contains Full Path of the file to be send but when i add more than one file to array it wont send anything. It is disturbing me from a whole month Any help or suggestions will be really appreciated
Here is my Sender's Code
@Override
public void run() {
Intent intent = getIntent();
Bundle bundle = intent.getExtras(); //Receiving Intent from other Activity
ArrayList send = (ArrayList) bundle.getParcelableArrayList("send"); //Full path of file in ArrayList
ArrayList name = (ArrayList) bundle.getParcelableArrayList("name"); //Name of the file if Available
String con = send.toString();
String convert = con.replaceAll("\\[", "").replaceAll("\\]","");
file = new File(convert);
byte[] bytes = new byte[(int) file.length()];
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new FileInputStream(file));
DataInputStream dis = new DataInputStream(bis);
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
if (name == null){
dos.writeUTF(file.getName());
}else {
String conname = name.toString();
String convertname = conname.replaceAll("\\[", "").replaceAll("\\]","");
dos.writeUTF(convertname + ".apk");
}
int read;
while ((read = dis.read(bytes)) != -1){
dos.write(bytes,0,read);
}
socket.close();
}
Here is the receiver's
@Override
public void run() {
Socket socket = null;
int bytesRead;
InputStream in;
int bufferSize = 0;
try {
socket = new Socket(dstAddress, dstPort);
bufferSize = socket.getReceiveBufferSize();
in = socket.getInputStream();
DataInputStream clientData = new DataInputStream(in);
String fileName = clientData.readUTF();
File file = new File(Environment.getExternalStorageDirectory() + fileName); //Name and place of file to be save
OutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[bufferSize];
int read;
while ((read = clientData.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
socket.close();
}
New Sender's code
public void run() {
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
ArrayList send = (ArrayList) bundle.getParcelableArrayList("send");
ArrayList name = (ArrayList) bundle.getParcelableArrayList("name");
BufferedInputStream bis;
byte[] bytes = new byte[8192];
try {
//bis = new BufferedInputStream(new FileInputStream(file));
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
for (int i =0; i<send.size();i++){
String filePath = send.get(i).toString();
File file = new File(filePath);
if (name!=null){ //check if name Array is empty or not
dos.writeUTF(String.valueOf(name.get(i)));
}else {
dos.writeUTF(file.getName());
}
dos.writeInt(file.size());
};
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(new BufferedInputStream(fis));
int read;
while ((read = dis.read(bytes)) != -1){
dos.write(bytes,0,read);
}
fis.close();
dos.flush();
os.flush();
socket.close();
New Receiver's code
try {
socket = new Socket(dstAddress, dstPort);
bufferSize = socket.getReceiveBufferSize();
in = socket.getInputStream();
DataInputStream clientData = new DataInputStream(in);
String fileName = clientData.readUTF();
int filelength = clientData.readInt();
File file = new File(Environment.getExternalStorageDirectory() + fileName); //Name and place of file to be save
OutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[8192];
int read;
while ((read = clientData.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
socket.close();
}