Search code examples
javaandroidandroid-studioandroid-activityandroid-bitmap

Android - How to pass a Bitmap in a constractor?


EDITED

I have this class:

    public class Item {

        private Bitmap image;

        public Item(Bitmap image) {
            this.image = image;
        }

I have also the main activity:

public class MainActivity extends AppCompatActivity {

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

        //the question is about this line
        Item item = new Item(???);

    }
}

How to call the constructor of the Item From MainActivity (inside onCreate)? I do not know how to refer to a Bitmap from Resources.

The image I want to pass to the constructor is located in: app >> res >> mipmap >> cat.png


Solution

  • Bitmap image = BitmapFactory.decodeResource(getResources() , R.drawable.cat);