简体   繁体   English

Android设置位图图像

[英]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. 我将编码设置如下,并链接了A类和DrawView类。

Question: 题:

The code reports error "The method setImageBitmap(Bitmap) is undefined for the type Bitmap" in DrawView Class for the line 该代码在该行的DrawView Class报告错误“对于类型Bitmap,未定义setImageBitmap(Bitmap)方法”

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

, I do not know how to load a picture to Bitmap. ,我不知道如何将图片加载到位图。

in Class A: 在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: 在类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. 您正在调用Bitmap类上不存在的方法。 That method is found on framework widgets like ImageView and ImageButton . 可以在诸如ImageViewImageButton框架小部件上找到该方法。 BitmapFactory returns a Bitmap already, so just assign the instance. BitmapFactory已经返回了Bitmap ,因此只需分配实例即可。

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? 为此,它应该是ImageView,不是吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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