title: Android can't display some of the images transferred over socket
body: <p>My app sends some text and an image to the other devices in the lan. It looks like images are transmitted and received successfully (I don't get any exception at all) but somehow, some of the images are not displayed well or not displayed at all in the imageView of a CardViewAdapter.</p>
  
  <p>When I receive the image, I create the file and save it with <strong>createFile</strong> method. I transmit also image type and save it with this extension. I don't know what it depends on, but I couldn't resolve it.</p>
  
  <p><strong>Class that send image over tcp:</strong></p>
  
  <pre><code>    public class ImageTransferHandler implements Runnable{
          private static final String TAG = "ImageTransferHandler";
          private static final int SERVER_PORT = 8700;
          private Socket socket;
          private Context context;
          private String hostIpAddress;
          private Bar bar;
  
          public ClientThreadProcessor(String hostIpAddress, Context context, Bar bar){
              this.context = context;
              this.hostIpAddress = hostIpAddress;
              this.Bar = bar;
          }
  
          private Socket getSocket(String hostIpAddress) {
                  InetAddress serverAddr = InetAddress.getByName(hostIpAddress);
                  socket = new Socket(serverAddr, SERVER_PORT);
              return socket;
          }
  
          private void closeSocket(Socket socket) {
                      socket.close();
          }
  
          @Override
          public void run() {
  
              socket = getSocket(hostIpAddress);
  
              OutputStream outputStream = socket.getOutputStream();
  
  
              PrintWriter output = new PrintWriter(new BufferedWriter(
                                              new OutputStreamWriter(outputStream)), true);
  
              output.println(bar.getImageInfo().isCamera());
              Uri imageUri = Uri.parse(bar.getImageInfo().getPath());
              String mimeType = ImagePicker.getMimeType(context, imageUri);
              String ext = mimeType.substring(mimeType.lastIndexOf("/") + 1);
              if(ext == null) ext = "bmp";
              output.println(ext);
              BufferedOutputStream bout = new BufferedOutputStream(outputStream);
              String realPath = ImagePicker.getRealPathFromUri(context, imageUri);
  
              InputStream input = new FileInputStream(realPath);
  
              byte[] buffer = new byte[1024];
              int len;
              while ((len = input.read(buffer)) != -1) {
                  bout.write(buffer);
              }
              input.close();
              bout.close();
              Log.d(TAG, "Image sent over network and stream is closed.");                                        
  
              output.close();
  
              closeSocket(socket);
  
          }
  
      }
  </code></pre>
  
  <p><strong>Class that receives image over tcp</strong></p>
  
  <pre><code>public class ImageReceiveHandler implements Runnable{
          private static String TAG = "ImageReceiveHandler";
          private Socket socket;
          private Context context;
          private BufferedReader inputBufferedReader;
          private PrintWriter outputPrintWriter;
          private Bar Bar;
  
  
          public ClientHandler(Socket socket, Context context){
              this.socket = socket;
              this.context = context;
          }
  
          @Override
          public void run() {
  
                  InputStream input = socket.getInputStream();
                  inputBufferedReader = new BufferedReader(
                                          new InputStreamReader(input));
  
                  outputPrintWriter = new PrintWriter(new BufferedWriter(
                          new OutputStreamWriter(socket.getOutputStream())), true);
  
                  boolean isCamera = Boolean.valueOf(inputBufferedReader.readLine());
                  String ext = inputBufferedReader.readLine();
  
                  File imageFile = ImagePicker.createFile(context, ImagePicker.isExternalStorageWritable(), ext);
  
                  if (imageFile != null) {
                      Uri imageUri = Uri.fromFile(imageFile);
                      Log.d(TAG, "imageUri for received image: " + imageUri.toString());
  
                      BufferedInputStream bufin = null;
                      FileOutputStream output = null;
  
                      bufin = new BufferedInputStream(input);
                      output = new FileOutputStream(imageFile);
  
                      byte[] buffer = new byte[1024];
                      int len;
                      while((len = bufin.read(buffer)) != -1){
                         output.write(buffer);
                         output.flush();
                      }
  
                      output.close();
                      bufin.close();
  
                      Log.d(TAG, "Image is received and saved. File length: " + imageFile.length());
  
                      ImageInfo info = new ImageInfo(imageUri.toString(), isCamera);
                      Bar = new Bar(info);
  
                      addNotification(Bar, hostAddress);
                  }
                  else
                      Log.d(TAG, "ImagePicker.createFile returns NULL");
  
  
                  inputBufferedReader.close();
                  outputPrintWriter.close();
          }
  
  
          public File createFile(Context context, boolean external, String ext) {
              File direct;
  
              if(external)
                  direct = new File(Environment.getExternalStorageDirectory() + "/myo_images");
              else
                  direct = new File(context.getFilesDir() + "/myo_images");
  
  
              if (!direct.exists()) {
                  File imagesDir;
                  if(external)
                      imagesDir = new File("/sdcard/myo_images/");
                  else
                      imagesDir = new File("/myo_images");
  
                  imagesDir.mkdirs();
              }
  
              String timestamp =  new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  
              File file = null;
  
              if(external) 
                   file = File.createTempFile("myo_" + timestamp, "." + ext, direct);
              else 
                   file = File.createTempFile("myo_" + timestamp, "." + ext, direct);
  
              return file;
          }
      }
  </code></pre>
  
  <p><strong>Adapter class which should visualize images</strong></p>
  
  <pre><code>public class BarsCardViewAdapter extends RecyclerView.Adapter&lt;BarsCardViewAdapter.CardViewHolder&gt; {
  
          private List&lt;BarData&gt; activeBars;
          private final static String TAG = "CARDVIEW_ADAPTER";
  
  
          public BarsCardViewAdapter(List&lt;BarData&gt; Bars){
              activeBars = Bars;
          }
  
          public static class CardViewHolder extends RecyclerView.ViewHolder {
              private ActiveBarsListItemBinding listItemBinding;
  
              public CardViewHolder(View v) {
                  super(v);
                  listItemBinding = DataBindingUtil.bind(v);
              }
  
              public ActiveBarsListItemBinding getBinding(){
                  return listItemBinding;
              }
  
          }
  
          @Override
          public BarsCardViewAdapter.CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
              View v = LayoutInflater.from(parent.getContext())
                      .inflate(R.layout.bars_list_item, parent, false);
  
              CardViewHolder holder = new CardViewHolder(v);
              return holder;
          }
  
  
  
          @Override
          public void onBindViewHolder(final BarsCardViewAdapter.CardViewHolder holder, final int position) {
              holder.getBinding().imageView.setVisibility(View.GONE);
              final Bar bar  = activeBars.get(position);
              final ImageInfo imageInfo = bar.getImageInfo();
              final LinearLayout rLayout = holder.getBinding().listItemLayout;
  
  
              ImageInfo imageInfo = BarData.getImageInfo();
              Log.d(TAG, "imagePath: " + Uri.parse(imageInfo.getPath()));
              holder.getBinding().imageView.setVisibility(View.VISIBLE);
              Picasso.with(rLayout.getContext()).
                          load(imageInfo.getPath()).
                          transform(new CropSquareTransformation()).
                          into(holder.getBinding().imageView);
  
  
              holder.getBinding().setVariable(BR.Bar, bar);
              holder.getBinding().executePendingBindings();
          }
  
          @Override
          public int getItemCount() {
              return activeBars.size();
          }
  
  
  
      }
  </code></pre>