title: Sending base64 encoded image to server using HttpUrlConnection Android
body: <p>I'm trying to send base64 encoded images to a server using HttpUrlConnection. The problem I'm having is that most images gets sent successfully, however some generate a FileNotFound exception. My code for encoding the image can be found below.</p>
  
  <pre><code>public static String encodeImage(Bitmap thumbnail) {
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, baos);
              byte[] b = baos.toByteArray();
              String imageEncoded = Base64.encodeToString(b,Base64.URL_SAFE);
              return imageEncoded;
          }
  </code></pre>
  
  <p>When I change the line:</p>
  
  <pre><code>String imageEncoded = Base64.encodeToString(b,Base64.URL_SAFE);
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
  </code></pre>
  
  <p>then most images generate a FileNotFoundException and some gets sent to the server successfully.</p>
  
  <p>below is the code for my HttpUrlConnection:</p>
  
  <pre><code>public class HttpManager {
  
      public static String getData(RequestPackage p) {
  
          BufferedReader reader = null;
          String uri = p.getUri();
          if (p.getMethod().equals("GET")) {
              uri += "?" + p.getEncodedParams();
          }
  
          try {
              URL url = new URL(uri);
              HttpURLConnection con = (HttpURLConnection) url.openConnection();
              con.setRequestMethod(p.getMethod());
  
  
              if (p.getMethod().equals("POST")) {
                  con.setDoOutput(true);
                  OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
                  writer.write(p.getEncodedParams()); //Url encoded parameters
                  writer.flush();
              }
  
              StringBuilder sb = new StringBuilder();
              reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
  
              String line;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
  
              return sb.toString();
  
          } catch (Exception e) {
              e.printStackTrace();
              return null;
          } finally {
              if (reader != null) {
                  try {
                      reader.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                      return null;
                  }
              }
          }
  
      }
  
  }
  </code></pre>
  
  <p>Any help will be appreciated.
  Thanks</p>