如何采取从图库部件,并显示在另一活动所选择的图像?
我想从画廊的Widget加载所选图像并将其加载到另一个活动,我才会真正AP preciate帮助!
I am trying to load a selected image from Gallery Widget and load it in another activity, I will really appreciate help!
基本上它是关于采取我在库中选择,并在 OnItemClick
的意图的图像或我不`吨知道要查看图像中的其它活动。
Basically it is about take the image that I select in the Gallery and make in the OnItemClick
an Intent or I don`t know to view the image in another Activity.
类是这样的:
public class Motos extends Activity implements OnClickListener, OnItemSelectedListener, OnItemClickListener{
Gallery g;
Integer[] images = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4 };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.moto);
g = (Gallery) findViewById(R.id.gallery1);
g.setOnItemClickListener(this);
g.setSpacing(2);
}
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return images.length;
}
public Object getItem(int position) {
return images[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(images[position]);
i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//i.setLayoutParams(new Gallery.LayoutParams(136, 88));
return i;
}
private Context mContext;
}
@Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
// TODO Auto-generated method stub
}
}
嗯,貌似你可以通过执行获得所选图像的资源ID如下:
Well, looks like you can get the resource ID of the selected image by doing the following:
@Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
int selectedId = images[position];
}
您可以通过所选择的资源ID另一个活动
是这样的:
You could pass that selected resource id to another Activity
like this:
Intent intent = new Intent(Vehiculos.this, OtherActivity.class);
intent.putExtra("imageId", selectedId);
startActivity(intent);
,然后检索的ID在你的其他活动
是这样的:
Intent intent = getIntent();
int imageId = intent.getIntExtra("imageId", -1); // -1 would be a default value
这是一个粗略的例子 - 你可能需要添加一些错误检查
This is a rough example - you will probably need to add some error-checking.