【发布时间】: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