【问题标题】:Java android, getting image from gallery and show it on screen (error)Java android,从图库中获取图像并将其显示在屏幕上(错误)
【发布时间】:2012-02-01 10:44:15
【问题描述】:

我第一次发布问题,所以就这样吧。
我想按下一个按钮,它会打开图库,选择一张图片,然后将其显示在屏幕上的某个位置(布局)。

到此为止:

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

【问题讨论】:

  • 而不是评论你的大括号,IMO 它更具可读性(并且更容易维护!)只是使缩进匹配。
  • 如果您解决了,请接受您认为正确的答案..

标签: java android imageview gallery


【解决方案1】:

我认为这条线会导致错误..

ImageView.setImageBitmap(bMap);

这里ImageView 是一个类,而不是这个你必须创建它的一个对象然后使用setImageBitmap 给它。,

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

或者,如果您已经在 Activity 中定义了 ImageView 对象,那么就使用它..

【讨论】:

  • 感谢这工作。 ImageView img = (ImageView)findViewById(R.id.); img.setImageBitmap(bMap);谢谢,当然是你们俩! [已解决]
  • @Bigflow,不要将问题标记为“已解决”,而是接受答案。更多信息在FAQ
【解决方案2】:

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

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

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

【讨论】:

  • 感谢这工作。它还没有在屏幕上显示,但错误消失了,应用程序没有崩溃,感谢快速有用的回复
  • 您应该将其标记为答案,这样其他人也会受益。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-22
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
  • 2012-03-18
相关资源
最近更新 更多