How to correctly obtain the Context into a class that extends Fragment?
<p>I am absolutly new in Android and I am finding some dificulties to do the following thing related to the Context.</p>

<p>So I have an utility class that contain an utility method that create and return a <strong>Bitmap</strong> image, this is the code of my class:</p>

<pre><code>public class ImgUtility {

    /**
     * Method that create the images related to the difficulty of a recepy
     * @param context
     * @param difficulty that represent the number of chef_hat_ok into the final image
     * @return a Bitmap representing the difficult of a recepy
     */
    public static Bitmap createRankingImg(Context context, int difficulty) {

        // Create a Bitmap image starting from the star.png into the "/res/drawable/" directory:
        Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.chef_hat_ok);


        // Create a new image bitmap having width to hold 5 star.png image:
        Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth() * 5, myBitmap.getHeight(), Bitmap.Config.RGB_565);

        /* Attach a brand new canvas to this new Bitmap.
           The Canvas class holds the "draw" calls. To draw something, you need 4 basic components:
           1) a Bitmap to hold the pixels.
           2) a Canvas to host the draw calls (writing into the bitmap).
           3) a drawing primitive (e.g. Rect, Path, text, Bitmap).
           4) a paint (to describe the colors and styles for the drawing).
         */
        Canvas tempCanvas = new Canvas(tempBitmap);

        // Draw the image bitmap into the cavas:
        tempCanvas.drawBitmap(myBitmap, 0, 0, null);
        tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth(), 0, null);
        tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 2, 0, null);
        tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 3, 0, null);
        tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 4, 0, null);

        return tempBitmap;

    }

}
</code></pre>

<p>As you can see this class contains the <strong>createRankingImg</strong> that take the <strong>Context</strong> object as parameter and use it to create an image. This object is used to retrieve an image from the resources (into the <strong>BitmapFactory.decodeResource()</strong> method). What exactly represent the <strong>Context</strong> object into an Android application?</p>

<p>I know that to obtain the context into an <strong>activity class</strong> I can use the <strong>getResources()</strong> method.</p>

<p>My problem is that I have to obtain the context into a class that exetends <strong>Fragment</strong>.</p>

<p>I have something like this:</p>

<pre><code>public class ScreenSlidePageFragment extends Fragment {

    .......................................................................
    .......................................................................
    .......................................................................

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        .....................................................................
        .....................................................................
        .....................................................................

        switch (mPageNumber + 1) {

            case 1:
                imgSlideView.setImageResource(R.drawable.carbonara);
                ((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.carbonara));

                ImageView difficultyContainerImageView1 = (ImageView) rootView.findViewById(R.id.difficultyContainer);

                difficultyContainerImageView1.setImageDrawable(new BitmapDrawable(getResources(),     ImgUtility.createRankingImg(getApplicationContext(), 3)));

                break;

        .....................................................................
        .....................................................................
        .....................................................................

        }

        return rootView;
}
</code></pre>

<p>My problem is that when in the previous fragment class I call the method</p>

<pre><code>ImgUtility.createRankingImg(getResources(), 3)
</code></pre>

<p>passing to it the <strong>getResources()</strong> output (that I thinked give me the <strong>Context</strong>, the IDE give me the following error message:</p>

<blockquote>
  <p>Wrong 1st argument type. Found: 'android.content.res.Resources',
  required: 'android.content.Context'</p>
</blockquote>

<p>So it seems to me that into a class that extends a <strong>Fragment</strong> and not an <strong>Activity</strong> the <strong>getResources()</strong> method return a <strong>Resources</strong> object instead a <strong>Context</strong> object (as done into an Activity class). Is it true? Why?</p>

<p>How can I obtain the <strong>Context</strong> inside a class that extends <strong>Fragment</strong>? And what exactly represent the <strong>Context</strong> in an Android app?</p>