I'm currently working for Android OS for the first time.
I want to load my saved images from path (/res/drawables) into my gallery. Can anyone guide me how to go about it.
The code I've written is working fine but it takes hard coded images. I want all the images that are there in my res/drawable folder to be loaded on page load.
Part of my code is:
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(MyPhotos.this));
g.setFadingEdgeLength(40);
// Set a item click listener, and just Toast the clicked position
g.setOnItemClickListener(new OnItemClickListener()
{
@SuppressWarnings("unchecked")
public void onItemClick(AdapterView parent, View v, int position, long id)
{
//---display the images selected---
//ImageView imageView = (ImageView) findViewById(R.id.image1);
//imageView.setImageResource(position);
Toast.makeText(MyPhotos.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
public class ImageAdapter extends BaseAdapter
{
int mGalleryItemBackground;
public ImageAdapter(Context c)
{
mContext = c;
// See res/values/attrs.xml for the <declare-styleable> that defines
// Gallery1.
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount()
{
return mImageIds.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView i = new ImageView(mContext);
i.setImageURI(mUrls[position]);
//i.setImageResource(mImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(100, 88));
// The preferred Gallery item background
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
private Context mContext;
private Integer[] mImageIds = {
R.drawable.flowers,
R.drawable.mother,
R.drawable.hands,
R.drawable.pink,
R.drawable.quotes,
R.drawable.saying,
R.drawable.sleepingbaby
};
}
Edited by Dogstopper:
This post has been edited by Dogstopper: 17 March 2010 - 06:56 AM

New Topic/Question
Reply



MultiQuote







|