title: Download a jpg image from the web saving it into a byte array on RAM
body: <p>I’m trying to download a jpg image from the web saving it into a byte array in order to have the compressed image on the ram until I have to show it, but I’m getting a black picture.</p>
  
  <p>What I’m doing is:</p>
  
  <pre><code>URL myFileUrl =null; 
  
  myFileUrl= new URL(fileUrl);
  
  HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
  conn.setDoInput(true);
  conn.connect();
  conn.setUseCaches(false);
  
  InputStream is = conn.getInputStream();
  
  byte[] ba = new byte[is.available()];
  </code></pre>
  
  <p>After that I’m keeping the array in the RAM until I have to use it.</p>
  
  <p>To place it, I do:</p>
  
  <pre><code>Bitmap bitmapFromByteArray = BitmapFactory.decodeByteArray(ba,0,ba.length);
  
  imageView1.setImageBitmap(bitmapFromByteArray);
  
  bitmapFromByteArray.recycle();
  </code></pre>
  
  <p>I solved the problem by doing </p>
  
  <pre><code>URL myFileUrl =null; 
  
  myFileUrl= new URL(fileUrl);
  
  HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
  conn.setDoInput(true);
  conn.connect();
  
  InputStream is = conn.getInputStream();
  
  Bitmap bm = BitmapFactory.decodeStream(is);
  
  int size = bm.getWidth() * bm.getHeight();
  
  ByteArrayOutputStream out = new ByteArrayOutputStream(size);
  bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
  
  byte[] ba2 = out.toByteArray();
  bm.recycle()
  </code></pre>
  
  <p>Buut by doing this I’m decoding the JPEG and then re-encoding the image multiple times lowering the quality of the picture and using the phone resources longer than necessary. Also, the bitmap I get after re-encoding is bigger than the original jpg image.</p>
  
  <p>Is there any way to get the first method to work?</p>
  
  <p>Thank you.</p>