我现在正在制作绘画应用,想问一下如何加载图片并设置为位图?
我已按如下方式设置编码,并链接A类和Class DrawView。
代码报告错误"方法setImageBitmap(Bitmap)未定义类型Bitmap"在DrawView Class
中为行
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
}
答案 0 :(得分:1)
您正在调用Bitmap
类中不存在的方法。该方法可以在ImageView
和ImageButton
等框架小部件上找到。 BitmapFactory
已经返回Bitmap
,因此只需分配实例。
bitmap = BitmapFactory.decodeFile(picturePath);
答案 1 :(得分:0)
decodeFile调用将从文件创建一个Bitmap。你的变量位图 - 它的类型是什么?对于那个调用它应该是一个ImageView,是吗?