android set Bitmap image-Collection of common programming errors

I am now making a painting apps, and would like to ask how to load a picture and set to a Bitmap?

I have set the coding as follows, and links Class A and Class DrawView.


The code reports error “The method setImageBitmap(Bitmap) is undefined for the type Bitmap” in DrawView Class for the line


, I do not know how to load a picture to Bitmap.

in Class A:

private DrawView drawView;

public void go_load_pic() 
    Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                
    startActivityForResult(i, RESULT_LOAD_IMAGE);   

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) 
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);


in Class DrawView:

public class DrawView extends View  // the main screen that is painted
   // used to determine whether user moved a finger enough to draw again   
   private static final float TOUCH_TOLERANCE = 10;

   private Bitmap bitmap; // drawing area for display or saving
   private Canvas bitmapCanvas; // used to draw on bitmap
   private Paint paintScreen; // use to draw bitmap onto screen
   private Paint paintLine; // used to draw lines onto bitmap
   private HashMap pathMap; // current Paths being drawn
   private HashMap previousPointMap; // current Points

public void load_pic(String picturePath) // load a picture from gallery
      bitmap.setImageBitmap(BitmapFactory.decodeFile(picturePath)); //ERROR LINE
      invalidate(); // refresh the screen
  1. You’re calling a method that doesn’t exist on the Bitmap class. That method is found on framework widgets like ImageView and ImageButton. BitmapFactory returns a Bitmap already, so just assign the instance.

    bitmap = BitmapFactory.decodeFile(picturePath);
  2. The decodeFile call will create a Bitmap from a file. Your variable bitmap- what’s its type? For that call it ought to be an ImageView, is it?

Originally posted 2013-11-26 18:02:59.