Java android,从图库获取图像并将其显示在屏幕上(错误)
我第一次发布问题,所以就这样吧。
我想按一个按钮,这样它就会打开图库,选择一张图片,然后将其显示在屏幕上的某个位置(布局)。
我现在已经明白了:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这一行会导致错误。
这里
ImageView
是一个类,而不是这个,你必须创建它的一个对象,然后使用setImageBitmap
。,或者如果你有已经在您的活动中定义了 ImageView 对象,然后只需使用它..
I think this line cause error..
Here
ImageView
is a class, instead of this you have to create a object of it then usesetImageBitmap
to it.,Or if you have already defined ImageView object in your activity then just use that..
你必须创建ImageView类的对象吗?例如:
或
You must create the object of ImageView class? For example:
or