title: How to get resource without context in Android?
body: <p>If I have different resource names like elephant, tiger and cat. And I want to create a method accepting the resource name and return the drawable object. Then I wrote this </p>
  
  <pre><code>public Drawable getDrawable(String name){
  int defaultResId=  ResourceOptimizer.getResId(name,R.drawable.class);
  return getResources().getDrawable(defaultResId);
  }
  </code></pre>
  
  <p>which the <code>ResourceOptimizer</code> is </p>
  
  <pre><code>public class ResourceOptimizer {
  public static int getResId(String resName, Class&lt;?&gt; c)         {
      try {
          Field idField = c.getDeclaredField(resName);
          return idField.getInt(idField);
      } catch (Exception e) {
          e.printStackTrace();
          return -1;
      }
  }
  }
  </code></pre>
  
  <p>But the problem is that <code>getResources()</code> needs to be called
  in an activity or fragment, otherwise you to pass the context.</p>
  
  <p>Passing the context might cause memory leak. Especially, when
  it has to be passed through several classes. I’m wondering if
  there is some way to get the resource by a better convenient way.
  like 
  <code>R.id.DRAWABLE_NAME</code></p>