title: why I can't encode and decode base64 with java and php?
body: <p>I encode Images on server with php and send the encoded strings to android via <code>get</code> request and decode it with java but It outputs bad base64 .</p>
  
  <p>so I decided to check the base64 string on online checker but the image doesn't appear is the problem with the php encoding ?</p>
  
  <p>here is the encoded image string :</p>
  
  <pre><code>aW1hZ2VzL21haW4vd3d3L25ldCAtIENvcHkucG5n
  </code></pre>
  
  <p><strong>PHP::</strong></p>
  
  <pre><code>&lt;?php
  
  
  require_once("config.php");
  
  if(isset($_GET["m"])) {
  
      $dirname = "images/main/";
      $arr = array();
  
      $conn = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);
  
      if(!$conn) {
          echo "Error connecting to database";
          exit();
      }
      if($stmt = $conn-&gt;prepare("SELECT name_ FROM projects")) {
          $stmt-&gt;execute();
          $stmt-&gt;bind_result($n);
          //$stmt-&gt;store_result();
          $result = $stmt-&gt;get_result();
          if($result-&gt;num_rows == 0) {
              echo "No Projects";
              $stmt-&gt;close();
              $conn-&gt;close();
              exit();
          }else {
              while ($row = $result-&gt;fetch_assoc()) {
                  $dirname = $dirname . $row["name_"] . "/";
                  $images = glob($dirname . "*.*", GLOB_BRACE);
                  foreach($images as $key =&gt; $image) {
                      $image = base64_encode($image);
                      //array_push($arr, $image);
                      $dirname = "images/main/";
                      echo $image;
                      echo "/n";
                      $image = "";
                  }
              }
              //echo "hi";//json_encode($arr);
          }
      }
  
      $stmt-&gt;close();
      $conn-&gt;close();
      exit();
  
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>ANDROID::</strong></p>
  
  <pre><code> @Override
      protected String doInBackground(String[] params) {
          String add = "http://10.0.2.2/wael/getimages.php?m=all";
          byte[] image = null;
          Bitmap real = null;
          String parsedString = "";
          BufferedReader bufferedReader = null;
          InputStream is = null;
          StringBuilder sb = null;
          HttpURLConnection httpConn = null;
          URLConnection conn = null;
          try {
              URL url = new URL(add);
              conn = url.openConnection();
              httpConn = (HttpURLConnection) conn;
              httpConn.setAllowUserInteraction(false);
              httpConn.setInstanceFollowRedirects(true);
              httpConn.setRequestMethod("GET");
              httpConn.connect();
              is = httpConn.getInputStream();
              sb = new StringBuilder();
              String line;
              bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
              bitmaps = new ArrayList&lt;&gt;();
              while ((line = bufferedReader.readLine()) != null) {
                  sb.append(line).append("\\n");
  
                  //image = Base64.decode(line, Base64.NO_PADDING);
                  //real = BitmapFactory.decodeByteArray(image, 0, image.length);
                  //bitmaps.add(real);
                  //image = null;
                  //real = null;
              }
              String[] lines = sb.toString().split("\\n");
              for(String s: lines){
                 image = Base64.decode(s, Base64.URL_SAFE);
                  real = BitmapFactory.decodeByteArray(image, 0, image.length);
                  bitmaps.add(real);
                  image = null;
                  real = null;
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
          finally {
              try {
                  if (is != null) {
                      is.close();
                  }
                  if (bufferedReader != null) {
                      bufferedReader.close();
                  }
                  if (httpConn != null) {
                      httpConn.disconnect();
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          return "done";
      }
  </code></pre>
  
  <p><strong>HINT::</strong> </p>
  
  <p>all types of modes in decoding using java are not working . for exmaple <code>URL_SAFE</code> , <code>DEFAULT</code> and etc arenot working all output the same bad base64</p>