我现在正在做一个绘画应用,请问如何加载图片并设置为Bitmap?
我已经设置了编码如下,并且链接了Class A和Class DrawView。
问题:
代码在 DrawView Class
中报告错误“The method setImageBitmap(Bitmap) is undefined for the type Bitmap” 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
}
最佳答案
您正在调用 Bitmap
类中不存在的方法。该方法可在 ImageView
和 ImageButton
等框架小部件上找到。 BitmapFactory
已经返回一个 Bitmap
,因此只需分配实例即可。
bitmap = BitmapFactory.decodeFile(picturePath);
关于Android设置位图图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14421780/