title: How to know if a byte array is a valid image
body: <p>Is there a way to know if a byte array is a valid image?</p>
  
  <p>I am taking a photograph, In OnActivityResult:</p>
  
  <pre><code>Bitmap imageBitmap = (Bitmap) extras.get("data");
  imageBitmap.compress(Bitmap.CompressFormat.JPEG, 75, out);
  //arrayFotos is an ArrayList
  arrayFotos.add(new BeanFotos("", imageBitmap, lastKnwonLocation.getLatitude(), lastKnwonLocation.getLongitude()));
  </code></pre>
  
  <p>I am storing it in database:</p>
  
  <pre><code>//listaFotos and arrayFotos are the same array, but with different names, because I am storing it in another class in my app
  for(int i=0;i&lt;listaFotos.size();i++){
  values.clear();
  values.put("codficha", codficha);
  int bytes=listaFotos.get(i).getFoto().getByteCount();
  ByteBuffer buffer = ByteBuffer.allocate(bytes);
  listaFotos.get(i).getFoto().copyPixelsToBuffer(buffer);
  values.put("foto", buffer.array());
  db.insert("fotos", null, values);
  }
  </code></pre>
  
  <p>Later, I need that image to be shown:</p>
  
  <pre><code>byte[] arrayFoto = csr2.getBlob(0);
  Bitmap fotoBitmap=BitmapFactory.decodeByteArray(arrayFoto, 0, arrayFoto.length);
  </code></pre>
  
  <p>The problem is, no matter what i do, the decodeByteArray method always returns null. I have tried with external libraries, and it is always null, so I am thinking maybe I am storing it in the wrong way. The arrayFoto variable is NOT null, I am receiving bytes (84584, to be exact) but the decodeByteArray method returns null. </p>
  
  <p>So, is there a way for me to be sure that the byte array is a correct image?</p>
  
  <p>Thanks.</p>