title: Image quality changes while encoding/decoding image to base64 string
body: <p>In my android app I am having user select an image file from his/her mobile, display it in a textview and then I am saving it in a json string before navigating to next page(navigating to next page is same as reloading the activity with new set of data). </p>
  
  <p>I am using the below two methods for converting a Drawable to encoded string and to decode the string back to Drawable.</p>
  
  <pre><code>public String encodeImageToString(Drawable d) throws Exception{
      Bitmap bm = ((BitmapDrawable) d).getBitmap();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);     byte[] byteArrayImage = baos.toByteArray();
      Toast.makeText(getBaseContext(), "Size After encoding to string:"+byteArrayImage.length, Toast.LENGTH_LONG).show();
      String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
      bm=null;
      baos.close();
      baos=null;
      return encodedImage;
  }
  
  public Drawable decodeStringToImage(String encodedImage){   
      byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
      Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
      Drawable d = new BitmapDrawable(getResources(),decodedByte);
      Toast.makeText(getBaseContext(), "Size after decoding string to image: " + decodedByte.getByteCount(), Toast.LENGTH_LONG).show();
  
  
  decodedByte = null;
      return d;
  
  }
  </code></pre>
  
  <p>The issue I am facing is that every time I navigate back to the page, the image quality decreases. And also the bytearray size as I display in encodeImageToString() using the Toast, keep on increasing with every reload.</p>
  
  <p>My sequence of action is
  -load image from selected file into textview ( compress the image while loading. I have not shared that code above)
  -Once user navigates to next page, I save the textview image into base64 encoded String. Here I call method encodeImageToString(Drawable d) and convert image to string and store it in JSON String
  -Once user navigates back to the page, I retrieve the string from the JSON String and then call decodeStringToImage(String s) to get back the Drawable. I then display that Drawable into textview.</p>
  
  <p>The problem is that when image gets reloaded into the textview the quality decreases. Every reloaded decreases the quality. Also as I checked the bytearray size as I display in encodeImageToString() keeps on increasing.</p>
  
  <p>Can someone please suggest if there is anything I am missing here. Reloading thhe image to string and then string back to image should have no impact on quality or size of the image. But that is not so in this case.</p>