I am trying to send image from server socket to client socket so i am getting correct image path and converting into bytes and from client side i am receiving data also but after saving to storage its not opening the file , it showing that invalid image.
Image size is correct but its dimension is -1 X -1
Server side code onActivityResult i am getting the image URI
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
String path = uri.getPath();
Log.e(TAG, path);
if (mBoundService != null) {
mSOcket = mBoundService.getSocket();
mSocketArraylist = mBoundService.getSocketArrayList();
if (mSocketArraylist != null) {
for (int i = 0; i < mSocketArraylist.size(); i++) {
Socket mSocket = mSocketArraylist.get(i);
OutputStream os = null;
try {
os = mSocket.getOutputStream();
ContentResolver cr = mContext.getContentResolver();
InputStream is = null;
try {
is = cr.openInputStream(Uri.parse(uri.toString()));
} catch (FileNotFoundException e) {
Log.d(TAG, e.toString());
}
copyFile(is, os);
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
Log.e("Socket value", "Socket is null");
}
}
}
}
Client side code
if (wifiInfo != null) {
if (!wifiInfo.isGroupOwner) {
String host = wifiInfo.groupOwnerAddress.getHostAddress();
Socket clientSocket = new Socket();
OutputStream os = null;
try {
clientSocket.bind(null);
clientSocket.setKeepAlive(true);
clientSocket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);
os = clientSocket.getOutputStream();
PrintWriter pw = new PrintWriter(os);
InputStream is = clientSocket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String data = br.readLine();
Log.e(TAG, data + "");
final File f = new File(Environment.getExternalStorageDirectory() + "/"
+ ClientService.this.getPackageName() + "/wifip2pshared-" + System.currentTimeMillis()
+ ".jpg");
File dirs = new File(f.getParent());
if (!dirs.exists())
dirs.mkdirs();
f.createNewFile();
String line;
while (true) {
Log.d(TAG, "server: copying files " + f.toString());
InputStream inputstream = clientSocket.getInputStream();
copyFile(inputstream, new FileOutputStream(f));
break;
}
signalActivity(f.getAbsolutePath());
Thread.sleep(500000);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
copyFile method implementation is given below
public static boolean copyFile(InputStream inputStream, OutputStream out) {
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.close();
inputStream.close();
} catch (IOException e) {
Log.d(TAG, e.toString());
return false;
}
return true;
}
So any one can help me out in this.