title: How to load an image from drawable and convert to a bitmap
body: <p>I have an image in the drawable folder and I want to convert into a bitmap and then convert to byte[] to store into a database, I can convert the bitmap to a byte array but I'm not able to get the image from drawable since <code>Bitmap photo = BitmapFactory.decodeResource(getResources(), R.drawable.image);</code> returns null. How to do it?</p>
  
  <p>what i have for the moment </p>
  
  <pre><code>Bitmap photo = BitmapFactory.decodeResource(getResources(), R.drawable.image); //this returns null
  if(photo != null)
  byte[] bytes = getBitmapAsByteArray(photo);
  
  //this works
  DatabaseHelper databaseHelper = new DatabaseHelper(this);
  databaseHelper.add("DEFAULT",bytes);
  </code></pre>
  
  <p>UPDATE:
  when I set the bitmap to image with <code>photo.setImageBitmap(bitmap);</code> after it retrieves information from database it does not appear, seems that the store does not work</p>
  
  <p>I'm storing info into database like this</p>
  
  <pre><code>private static byte[] getBitmapAsByteArray(Bitmap bitmap) {
          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
          bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
          return outputStream.toByteArray();
      }
  
  public boolean addUserInformation(String username, byte[] picture){
          SQLiteDatabase db = this.getWritableDatabase();
          ContentValues values = new ContentValues();
          values.put("USERNAME", username);
          if(picture!=null) {
              values.put("PICTURE", picture);
          }
          id = db.insert(TABLE_NAME1, null, values);
          }
          db.close();
          if(id == -1)
              return false;
          else
              return true;
      }
  </code></pre>
  
  <p>and retrieve information like this</p>
  
  <pre><code>Cursor data = databaseHelper.getUserInformation();
          if(data.moveToFirst()) {
              mUsername = data.getString(1);
              bytes = data.getBlob(2);
              if(bytes!=null)
                  profilePhoto = BitmapFactory.decodeByteArray(bytes, 0, data.getBlob(2).length);
          }
  </code></pre>
  
  <p>with getUserInformation() inside database class</p>
  
  <pre><code>public Cursor getUserInformation(){
          SQLiteDatabase db = this.getReadableDatabase();
          String query = "SELECT * FROM " + TABLE_NAME1;
          Cursor c = db.rawQuery(query,null);
          return c;
      }
  </code></pre>