I've done Gallery of images displayed on emulator by static(i.e images from drawable
folder).Now i need to add some images into the list of gallery, dynamically from the local path(for ex.from E:/anim.jpeg like that).How can i do that?Thanks..
My gallery code is shown below..
public class GalleryAct extends Activity {
private Gallery gallery;
private ImageView imgView;
private Integer[] Imgid = {
R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7,
R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(Imgid[0]);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
imgView.setImageResource(Imgid[position]);
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return Imgid.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 imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
}
Write the file path where the image is saved.
Environment.getExternalStorageDirectory() gives path of sdcard.
If your image is too big size than bitmap will give error so for that you have to write below code to resize image. Pass the file in below function
In your case, you can try make your image array a dynamic list, ex: ArrayList. Upon the arrival of new item, add it to the list, and call notifyDataSetChanged() (method of the adapter), and your gallery list will be refreshed.
Depends on your case, I found that it is better to use AsyncTask here to update the list, and call notifyDataSetChanged.
The adapter class would looks similarly to this
Let me know if any errors, I am way to depending on IDE.