title: encode and decode base64 excpetion storing to sql server 2008 r2
body: <p>I have an android client, I want to send image from my android to my server, i convert the image to byte array, then they byte array to base64, i send the base64 string to server, i decode it in the server then stored it in my sql server 2008 r2.</p>
  
  <p><h3>my problem</h3>I can't store the string to my database then retrieve it correctly, I didn't get any exception and I get result but it seems it is the wrong result.</p>
  
  <p>i conclude that by doing this.</p>
  
  <p>1- i send the base64 from android to server and saved the string retrieved in a static variable.
  2- i asked the server to retrive the static string, i get the image in the android back.
  3- but when i asked the server to retrieve the image that i supposed i saved it in the database i got wrong image ,actullly i get result but this result can't be decoded again.</p>
  
  <p>I will tell you my code for insert and retrive the image in database, pleaes tell me what am i doing wrong,</p>
  
  <h3>Stored to database</h3>
  
  <pre><code>void uploadImage(String image) {
  Connection con = Database.getConnection();
          CallableStatement callableStatement = null;
          try {
              callableStatement = con
                      .prepareCall("{call insertRestaurantFoodImage(?,?)}");
              ByteArrayInputStream b = new ByteArrayInputStream(
                      stringImage.getBytes());
              callableStatement.setInt(1, ID);
              ;
              callableStatement.setBinaryStream(2, b,
                      stringImage.getBytes().length);
              callableStatement.executeUpdate();
          } catch (SQLException e) {
              e.printStackTrace();
          }
  }
  </code></pre>
  
  <p><strong>the stored procedure to insert the image is</strong> </p>
  
  <pre><code>ALTER PROCEDURE dbo.insertRestaurantFoodImage
      (
      @restaurantFoodID INT,
      @image VARBINARY(MAX)
      )
  AS
  BEGIN
      SET NOCOUNT ON
      UPDATE Food_Restaurant
      SET [image] = @image
      WHERE ID = @restaurantFoodID
  END
  </code></pre>
  
  <h3>Retrieve from database</h3>
  
  <pre><code>Connection con = Database.getConnection();
              CallableStatement callableStatement = null;
              try {
                  callableStatement = con
                          .prepareCall("{call getRestaurantFoodImage(?,?)}");
                  callableStatement.setInt(1, getID());
                  callableStatement.registerOutParameter(2,
                          java.sql.Types.VARBINARY);
                  callableStatement.execute();
                  byte[] bytes = callableStatement.getBytes(2);
                  image = new String(bytes);
              } catch (SQLException e) {
                  e.printStackTrace();
              }
  </code></pre>
  
  <p><strong>the stored procedure to retrive from database is :</strong></p>
  
  <pre><code>ALTER PROCEDURE dbo.getRestaurantFoodImage
  (
      @foodRestaurantID INT,
      @image VARBINARY(MAX) OUTPUT
      )
  AS
  BEGIN
      SET NOCOUNT ON
      SELECT @image = [image]
      FROM Food_Restaurant
      WHERE ID = @foodRestaurantID
  END
  </code></pre>
  
  <p><strong>maybe i talked too much, but in the previus question, a user told me that i have to not just put the code but i have to put my problem and what i am doing.</strong></p>
  
  <p>for those uses who asked me about how to encode and decode in android here is it:</p>
  
  <h3>encode</h3>
  
  <pre><code>private byte[] getBytesFromBitmap(Bitmap bitmap) {
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          bitmap.compress(CompressFormat.PNG, 70, stream);
          return stream.toByteArray();
      }
  
      private String getBase64(Bitmap bitmap) {
          String imgString = Base64.encodeToString(getBytesFromBitmap(bitmap),
                  Base64.NO_WRAP);
          return imgString;
      }
  </code></pre>
  
  <h3>decode</h3>
  
  <pre><code>String retrievedBase63 = client2
                  .getBaseURI("restaurantFoods/OneFood/" + 5 + "/getImage");
          //Log.d("image", image);
          byte[] decodedString = Base64.decode(retrievedBase63, Base64.DEFAULT);
          Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
                  decodedString.length);
  </code></pre>