8

I can't seem to reference my image in the drawable folder. I have the image there however i keep getting an error stating that I cannot convert an int to Drawable. my R.java generated file has the string in there for the image however it is set as "public static final int restaurant=0x7f020001;"

package com.CS3040.Places;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import com.CS3040.*;
import com.CS3040.Coursework.R;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;

public class PlaceOverlayItem extends OverlayItem {
    private final GeoPoint point;
    private final Place place;
    private final Drawable marker;

    public PlaceOverlayItem(Place p, String type) {
        super(p.getGeoPoint(), p.getName(), p.getFormatted_address());


        if(type.equals("restaurant")){ this.marker = R.drawable.restaurant;  }

        //super.setMarker(this.marker);
        this.point = p.getGeoPoint();
        this.place = p;
    }

    /**
     * @return the point
     */
    public GeoPoint getPoint() {
        return point;
    }

    /**
     * @return the place
     */
    public Place getPlace() {
        return place;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user1270217
  • 131
  • 2
  • 5
  • 9

4 Answers4

20

You need to do as follows:

marker = getResources().getDrawable(R.drawable.restaurant);

The reason you get the message "The method getResources() is undefined for the type PlaceOverlayItem" is because getResources() is a method inherited from Context class, so you got to call it from an Activity (or so) or pass the context to your method.

Hope this helps

Zadec
  • 449
  • 2
  • 6
7

I think you want something like this:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.restaurant);
this.marker = bitmap;

Or, using your solution:

marker = getResources().getDrawable(R.drawable.restaurant);
Sparky
  • 8,437
  • 1
  • 29
  • 41
  • 1
    Good answer! Remember to `recycle()` your bitmaps when you're finished with them. – Sleepybear Mar 14 '12 at 22:39
  • Thanks guys for you help, I'm still getting an error with the getResources. "The method getResources() is undefined for the type PlaceOverlayItem". – user1270217 Mar 14 '12 at 22:45
  • getResources() is a method on Activity. – Sparky Mar 15 '12 at 07:09
  • 1
    the `getResources().getDrawable(R.drawable.restaurant);` is deprecated and you might have to use something like `ContextCompat.getDrawable(getContext(), R.drawable.restaurant)` – sujay Feb 09 '16 at 02:00
0

Kotlin version

To convert the drawable into the Bitmap, just use this block of code

val largeIcon = BitmapFactory.decodeResource(applicationContext.resources, R.drawable.my_large_icon)
cigien
  • 57,834
  • 11
  • 73
  • 112
Prateek
  • 502
  • 5
  • 16
0

R.drawable.restaurant is a constant int which holds the id of the drawable resource resturant, it is not a Drawable object.

MByD
  • 135,866
  • 28
  • 264
  • 277