我想从图库中添加一张图片,当我按下创建播放器时,我希望将该图片添加到列表视图中的播放器名称旁边。
现在,我的代码从可绘制对象中获取图片,并且这些图片是在类型化数组中访问的。
如何更改我的代码以实现它以从图库中获取图片并将其显示在列表视图中?
任何帮助将不胜感激!谢谢
我的自定义适配器代码是:
public class PlayerImageAdapter extends ArrayAdapter<Player> {
private LayoutInflater mInflater;
private ArrayList<Player> players;
private TypedArray mIcons;
private int mViewResourceId;
public PlayerImageAdapter(Context ctx, int viewResourceId,
ArrayList<Player> players, TypedArray icons) {
super(ctx, viewResourceId, players);
mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
this.players = players;
mIcons = icons;
mViewResourceId = viewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.playerPic);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.playerName);
tv.setText(this.players.get(position).toString());
return convertView;
}
}
//and my code that calls it:
TypedArray icons = res.obtainTypedArray(R.array.player_pics);
adapter = new PlayerImageAdapter(ctx, R.layout.player_customlist,
dataStore.getPlayers(), icons);
My code that access the gallery is:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imagePlayer);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}