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
  • 4
    what is that you are doing. `getDrawable(button1);` did you look at the docs for the api?. Your question is not clear. – Raghunandan Feb 22 '14 at 10:51
  • i want a button which can share my images to social apps, this is what i want to do sir –  Feb 22 '14 at 10:52
  • where is your images?? – kalyan pvs Feb 22 '14 at 10:53
  • what you are doing is wrong. You already have the button. To share it to social apps like facebook. You need to look at their facebooksdk. The images i guess are stored in sdcard or in gallery. You will need to get the path of those images – Raghunandan Feb 22 '14 at 10:53
  • the images are in `drawable` folder inside `res`. –  Feb 22 '14 at 10:54
  • @Raghunandan sir i am new to development, i don't know how to give location to images :( –  Feb 22 '14 at 10:55
  • this link may useful to you http://stackoverflow.com/questions/7661875/how-to-use-share-image-using-sharing-intent-to-share-images-in-android?rq=1 – kalyan pvs Feb 22 '14 at 10:56
  • store the images in external storage and get the path of those images. Google search you will find many exmaples – Raghunandan Feb 22 '14 at 10:56
  • 3
    As a warning, do not insult people trying to help you. I've removed those comments. – Brad Larson Feb 22 '14 at 15:44

3 Answers3

3

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
  • 16,144
  • 6
  • 54
  • 81
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
  • 313
  • 4
  • 5
-2

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