我现在正在做一个绘画应用程序,想问一下如何加载图片并设置为位图?
我将编码设置如下,并链接A类和DrawView类。
问题:
代码报告错误“The method setImageBitmap(Bitmap) is undefined for the type Bitmap” in DrawView Class
for 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
}