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

发布于 2025-01-02 03:59:57 字数 1502 浏览 2 评论 0原文

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

我现在已经明白了:

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

First time I post a question, so here it goes.
I want to push on a button, so it opens the gallery, pick a picture, then shows it somewhere on the screen (layout).

I got this far by now:

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

There is some other code above it too, but here somewhere is the problem.

I get an error on the line ImageView.setImageBitmap(bMap);
The error:

Cannot make a static reference to the non-static method setImageBitmap(Bitmap) from the type ImageView

I searched a lot on the internet, and tried many things, but I can't resolve it.
Maybe it is really easy and I just don't see it.

I am beginner at Java android programming, used to program in C++.
So also some explanation about the error would be very nice :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

圈圈圆圆圈圈 2025-01-09 03:59:57

我认为这一行会导致错误。

ImageView.setImageBitmap(bMap);

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

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

或者如果你有已经在您的活动中定义了 ImageView 对象,然后只需使用它..

I think this line cause error..

ImageView.setImageBitmap(bMap);

Here ImageView is a class, instead of this you have to create a object of it then use setImageBitmap to it.,

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

Or if you have already defined ImageView object in your activity then just use that..

陈甜 2025-01-09 03:59:57

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

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

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

You must create the object of ImageView class? For example:

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

or

ImageView img = (ImageView)findViewById(R.id.<your image view id>);
img.setImageBitmap(bMap);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文