0

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.

Community
  • 1
  • 1
Jai Sharma
  • 469
  • 5
  • 19

3 Answers3

2

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.

Community
  • 1
  • 1
Adam S
  • 14,954
  • 6
  • 51
  • 79
0

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()

Samarth S
  • 113
  • 1
  • 5
-3

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