304

This seems simple, I am trying to set a bitmap image but from the resources, I have within the application in the drawable folder.

bm = BitmapFactory.decodeResource(null, R.id.image);

Is this correct?

W4R10CK
  • 5,502
  • 2
  • 19
  • 30
Beginner
  • 28,539
  • 63
  • 155
  • 235

6 Answers6

804

Assuming you are calling this in an Activity class

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

The first parameter, Resources, is required. It is normally obtainable in any Context (and subclasses like Activity).

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
xandy
  • 27,357
  • 8
  • 59
  • 64
  • 4
    I am getting null value of bitmap ,can u say me why i am getting null – gautam Nov 17 '17 at 07:02
  • yes i'm getting the same issue, null value of the bitmap. – Er.Rohit Sharma Mar 20 '18 at 12:00
  • 3
    I got the issue. I was trying to convert vector drawable into bitmap. So here is the cod to convert vector drawable into bitmap. – Er.Rohit Sharma Mar 20 '18 at 12:12
  • 2
    If **not** calling from an Activity class (e.g., if called from a data class) you could try: val myBitmap = BitmapFactory.decodeResource(Resources.getSystem(), R.drawable.your_image) <—Kotlin – Bikeboy Dec 21 '18 at 15:06
  • I was gettin null value of Bitmap because I was using a vector resource (it you use a `.png` for example it works) – dnhyde Feb 17 '22 at 16:32
40

Try this

This is from sdcard

ImageView image = (ImageView) findViewById(R.id.test_image);
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
image.setImageBitmap(bMap);

This is from resources

Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bengt
  • 14,011
  • 7
  • 48
  • 66
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
9

If the resource is showing and is a view, you can also capture it. Like a screenshot:

View rootView = ((View) findViewById(R.id.yourView)).getRootView();
rootView.setDrawingCacheEnabled(true);
rootView.layout(0, 0, rootView.getWidth(), rootView.getHeight());
rootView.buildDrawingCache();

Bitmap bm = Bitmap.createBitmap(rootView.getDrawingCache());

rootView.setDrawingCacheEnabled(false);

This actually grabs the whole layout but you can alter as you wish.

Anthony Graglia
  • 5,355
  • 5
  • 46
  • 75
3

If you have declare a bitmap object and you want to display it or store this bitmap object. but first you have to assign any image , and you may use the button click event, this code will only demonstrate that how to store the drawable image in bitmap Object.

Bitmap contact_pic = BitmapFactory.decodeResource(
                           v.getContext().getResources(),
                           R.drawable.android_logo
                     );

Now you can use this bitmap object, whether you want to store it, or to use it in google maps while drawing a pic on fixed latitude and longitude, or to use some where else

Simson
  • 3,373
  • 2
  • 24
  • 38
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
0

just replace this line

bm = BitmapFactory.decodeResource(null, R.id.image);

with

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.YourImageName);

I mean to say just change null value with getResources() If you use this code in any button or Image view click event just append getApplicationContext() before getResources()..

Ravi Makvana
  • 2,872
  • 2
  • 24
  • 38
0

Using this function you can get Image Bitmap. Just pass image url

 public Bitmap getBitmapFromURL(String strURL) {
      try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
      } catch (IOException e) {
        e.printStackTrace();
        return null;
      }
 }
pavel
  • 1,603
  • 22
  • 19