gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:09:46 25 4
gpt4 key购买 nike

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

我现在已经走到这一步了:

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); 这行出错错误:

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

网上查了很多,也试了很多,都解决不了。也许这真的很容易,只是我没有看到。

我是 Java android 编程的初学者,以前用 C++ 编程。因此,对错误的一些解释也将非常好:D

最佳答案

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

ImageView.setImageBitmap(bMap);

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

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

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

关于Java android,从图库中获取图像并将其显示在屏幕上(错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9094459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com