user3340335
user3340335

Reputation:

How to call getDrawable method

I am creating an image sharing app which uses a button to share images. I'm getting an unable to resolve the required method error in the code below:

public class ShareActivity extends Activity implements View.OnClickListener {

    Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);

        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(this);
    }
    private void button1Click()
    {
        BitmapDrawable bm = (BitmapDrawable) getDrawable(button1);
        Bitmap mysharebmp = bm.getBitmap();
        String path = Images.Media.insertImage(getContentResolver(),
                            mysharebmp, "MyImage", null);

On this line:

BitmapDrawable bm = (BitmapDrawable) getDrawable(button1);ShareActivity

I am getting The method getDrawable(Button) is undefined for the type ShareActivity. Why might I be getting this error? The full code is here.

Upvotes: 0

Views: 8234

Answers (3)

Samarth S
Samarth S

Reputation: 335

If you are trying to get the image displayed inside the button the consider using the function defined inside the image button object. I mean to say use button1.getDrawable()

Upvotes: 0

Adam S
Adam S

Reputation: 16394

It's extremely unclear as to what you want to do here, and I suspect you're going to encounter a number of other problems with this code. But the reason that you're getting the error "getDrawable is undefined for ShareActivity" is because the Activity class does not have a getDrawable method.

Instead of calling getDrawable, you need to get the app resources and then retrieve the drawable. What you're looking for is:

BitmapDrawable bm = (BitmapDrawable) getResources().getDrawable(R.drawable.my_bitmap);

Where you have some image called "my_bitmap.png" in /res/drawable/.

Edit Feb 7 2016: This answer is no longer correct. Context#getDrawable(int) was added in API 21 (Lollipop) which was released in November 2014. Note that you probably shouldn't be using it anyway.

Upvotes: 3

Florescent Ticker
Florescent Ticker

Reputation: 645

The getDrawable() method requires id of the resource and in this case you are passing Button. Try passing R.id.button1 Here is the documentation

Upvotes: -2

Related Questions