title: BitmapFactory.decodeStream returns null
body: <p>I know this question came up a lot, but I can't find any solution that helps me.  I'm downloading avatars from my webpage and save them on internal storage.</p>
  
  <p>In my activity:</p>
  
  <pre><code>            // check if user has an custom avatar
              if (!bet.getUser().getAvatar().isEmpty()) {
                  // now check if we already downloaded the avatar
                  InputStream fis = AvatarHandler.getAvatar(getApplicationContext(), bet.getUser().getID());
                  if (fis != null) {
                      Bitmap bmp = BitmapFactory.decodeStream(fis);
                      System.out.println(bmp); // is always null
                      avatarView.setImageBitmap(bmp);
                  }
                  else {
                      // if not, download it
                      System.out.println(bet.getUser().getAvatar());
                      downloadAvatar(bet.getUser().getAvatar());
                  }
              }
              else {
                  // set default avatar
                  avatarView.setImageResource(R.drawable.avatar);         
              }
  
  
  private void downloadAvatar(String url) {
      AsyncHttpClient client = new AsyncHttpClient();
      client.get(url, new BinaryHttpResponseHandler() {
          @Override
          public void onSuccess(byte[] fileData) {
              // save avatar on internal storageasd
              AvatarHandler.saveAvatar(getApplicationContext(), fileData, bet.getUser().getID());
  
              // display avatar
              Bitmap bmp = BitmapFactory.decodeByteArray(fileData, 0, fileData.length);
              avatarView.setImageBitmap(bmp);   
          }
      });     
  }
  </code></pre>
  
  <p>My AvatarHandler class</p>
  
  <pre><code>public class AvatarHandler {
      private static String fileName = "avatar-";
  
      /**
       * Returns the InputStream of the avatar of the given userID. null is returned if no avatar is stored.
       * @param userID
       * @return string
       */
      public static InputStream getAvatar(Context context, int userID) {
          try {
              FileInputStream fis = context.openFileInput(fileName + String.valueOf(userID));
              InputStreamReader isr = new InputStreamReader(fis);
              BufferedReader bufferedReader = new BufferedReader(isr);
  
              StringBuilder sb = new StringBuilder();
              String line;
              while ((line = bufferedReader.readLine()) != null) {
                  sb.append(line);
              }
  
              InputStream stream = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
  
              return stream;
          }
          catch (IOException e) {
              e.printStackTrace();
  
          }
  
          return null;
      }
  
      /**
       * Stores the given avatar on internal storage.
       * @param context
       * @param data
       * @param userID
       */
      public static void saveAvatar(Context context, byte[] data, int userID) {
          try {
              FileOutputStream fos = context.openFileOutput(fileName + String.valueOf(userID), Context.MODE_PRIVATE);
              fos.write(data);
              fos.close();
          }
          catch (IOException e) {
              e.printStackTrace();
          }
      }
      }
  </code></pre>