简体   繁体   中英

Android set Bitmap image

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.

Question:

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

bitmap.setImageBitmap(BitmapFactory.decodeFile(picturePath));

, 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);
        cursor.moveToFirst();

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

        drawView.load_pic(picturePath);     
    }   
}   

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<Integer, Path> pathMap; // current Paths being drawn
   private HashMap<Integer, Point> 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
   }

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM