Java android,从图库中获取图像并在屏幕上显示(错误)

时间:2012-02-01 10:44:15

标签: java android imageview gallery

我第一次发帖提问,所以就这样了 我想按下一个按钮,所以它打开画廊,选择一张图片,然后在屏幕上的某个地方显示(布局)。

我现在已经走了这么远:

public void FotoKiezen(View v) {
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 1);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case 1:
     {
      if (resultCode == RESULT_OK)
      {
        Uri photoUri = data.getData();
        if (photoUri != null)
        {
        try {
              String[] filePathColumn = {MediaStore.Images.Media.DATA};
              Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
         cursor.moveToFirst();
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
     String filePath = cursor.getString(columnIndex);
     cursor.close();
     Bitmap bMap = BitmapFactory.decodeFile(filePath);
     ImageView.setImageBitmap(bMap);


     }catch(Exception e)
      {}
      }
    }// resultCode
    }// case 1
    }// switch, request code
}// public void onActivityResult

上面还有其他一些代码,但这里有问题。

我在第ImageView.setImageBitmap(bMap);行上收到错误 错误:

  

无法从ImageView类型中对非静态方法setImageBitmap(Bitmap)进行静态引用

我在互联网上搜索了很多,并尝试了很多东西,但我无法解决它。 也许这很容易,我只是没有看到它。

我是Java android编程的初学者,习惯于用C ++编程。 所以关于错误的一些解释也会非常好:D

2 个答案:

答案 0 :(得分:2)

我认为这一行导致错误..

ImageView.setImageBitmap(bMap);

此处ImageView是一个类,而不是必须创建一个对象,然后使用setImageBitmap

ImageVIew mImageView = new ImageView(this)
mImageView.setImageBitmap(bMap);

或者,如果您已经在活动中定义了ImageView对象,那么只需使用..

答案 1 :(得分:1)

您必须创建ImageView类的对象吗?例如:

ImageView img = new ImageView(this);
img.setImageBitmap(bMap);

ImageView img = (ImageView)findViewById(R.id.<your image view id>);
img.setImageBitmap(bMap);
相关问题