【发布时间】:2012-03-19 20:57:47
【问题描述】:
我试图从我的手机厨房访问并选择一个图像到我的应用程序 imageView,使用 android。到目前为止,我设法设置到厨房并选择我想要使用的图像,唯一的问题是一旦我选择了我想要的图像,我就无法将它放到 imageview 上。问题似乎是我将 imageview 类与我的主类分开。我检查过,找不到解决方案。
这是 mediagalley 类的代码
公共类 MediaGallery 扩展 Activity { public static final int SELECT_IMAGE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == SELECT_IMAGE) {
Uri selectedImage = data.getData();
String path = getPath(selectedImage);
Bitmap bitmapImage = BitmapFactory.decodeFile(path);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bitmapImage);
}
}
public String getPath(Uri uri) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, filePathColumn, null,null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
return cursor.getString(columnIndex);
}
}
及主类代码
case R.id.gallery_button:
MediaGallery activ1 = new MediaGallery();
Intent gallery = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, 1);
activ1.onActivityResult(1, 1, gallery);
break;
您给予的任何帮助都会非常感激
【问题讨论】: