title: Copy inputStream to file and read from it
body: <p>I want to copy the content of an <a href="/questions/tagged/input-stream" class="post-tag" title="show questions tagged 'input-stream'" rel="tag">input-stream</a> which is actually a bitmap to a <a href="/questions/tagged/file-output-stream" class="post-tag" title="show questions tagged 'file-output-stream'" rel="tag">file-output-stream</a>. After that I want to pass the content of the file-output-stream to BitmapFactory.decodeStream. The result is that I am getting damaged images! my code for these things is the below.</p>
  
  <pre><code>private void copyInStreamToFile(InputStream is) {
      byte buf[] = new byte[1024];
      int len;
  
      FileOutputStream fos;
      try {
          fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
          while ((len = is.read(buf)) &gt; 0)
              fos.write(buf, 0, len);
          fos.close();
      } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
  }
  
  private static FileInputStream getStream() throws FileNotFoundException {
      return context.openFileInput(FILENAME);
  }
  </code></pre>
  
  <p>I am calling this inside the download method</p>
  
  <p>Bitmap downloadBitmap(String url) {
          final int IO_BUFFER_SIZE = 4 * 1024;</p>
  
  <pre><code>    // AndroidHttpClient is not allowed to be used from the main thread
      final HttpClient client = AndroidHttpClient.newInstance("Android");
      final HttpGet getRequest = new HttpGet(url);
  
      try {
          HttpResponse response = client.execute(getRequest);
          final int statusCode = response.getStatusLine().getStatusCode();
          if (statusCode != HttpStatus.SC_OK) {
              Log.w("ImageDownloader", "Error " + statusCode
                      + " while retrieving bitmap from " + url);
              return null;
          }
  
          final HttpEntity entity = response.getEntity();
          if (entity != null) {
              InputStream inputStream = null;
              try {
                  inputStream = entity.getContent();
                  copyInStreamToFile(new FlushedInputStream(inputStream));
  
                  DeviceProperties device = new DeviceProperties(activity);
                  return decodeBitampFromResource(device.getDeviceHeight(),
                          device.getDeviceWidth());
              } finally {
                  if (inputStream != null) {
                      inputStream.close();
                  }
                  entity.consumeContent();
              }
          }
  </code></pre>
  
  <p>and the last method which makes the decoding is this:</p>
  
  <pre><code>public static Bitmap decodeBitampFromResource(int reqWidth, int reqHeight) {
      // First decode with inJustDecodeBounds=true to check dimensions
      final BitmapFactory.Options options = new BitmapFactory.Options();
      FileInputStream fis;
      InputStream is;
      Bitmap bitmap = null;
      // kanonika tha vriskei to megethos tis photo alla kapoio problima
      // iparxei me ton sixronismo
      try {
          fis = getStream();
  
          options.inJustDecodeBounds = true;
  
          BitmapFactory.decodeStream(fis, null, options);
  
          // Calculate inSampleSize
          options.inSampleSize = calculateInSampleSize(options, reqWidth,
                  reqHeight);
  
  
          // Decode bitmap with inSampleSize set
          options.inJustDecodeBounds = false;
          fis.close();
          fis = getStream();
  
          bitmap = BitmapFactory.decodeStream(fis, null, options);
          fis.close();
  
      } catch (FileNotFoundException e) {
          e.printStackTrace();
  
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
  
      }
          return bitmap;
  
  
  }
  </code></pre>
  
  <p>Do I have to clear the content of the file-output-stream? As I know from android developers openFileOutput opens the file and override it's content or creates it if it doesn't exist.</p>