1

我现在正在做一个绘画应用程序,想问一下如何加载图片并设置为位图?

我将编码设置如下,并链接A类和DrawView类。

问题:

代码报告错误“The method setImageBitmap(Bitmap) is undefined for the type Bitmap” in DrawView Classfor the line

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

,我不知道如何将图片加载到Bitmap。

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

在类 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
   }
4

2 回答 2

1

您正在调用该类中不存在的方法Bitmap。该方法可以在框架小部件上找到,例如ImageViewImageButtonBitmapFactory已经返回 a Bitmap,所以只需分配实例。

bitmap = BitmapFactory.decodeFile(picturePath);
于 2013-01-20T05:03:46.703 回答
0

decodeFile 调用将从文件创建位图。你的变量位图——它的类型是什么?对于那个调用,它应该是一个 ImageView,是吗?

于 2013-01-20T04:59:10.633 回答