Android OS

Working with Gallery in Android OS

Page 1 of 1

4 Replies - 3520 Views - Last Post: 22 March 2010 - 09:19 PM

#1 Meetu Sinha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 10

Android OS

Posted 17 March 2010 - 12:43 AM

Hello Enggs

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: :code:

This post has been edited by Dogstopper: 17 March 2010 - 06:56 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Android OS

#2 Petsarve  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 17-February 10

Re: Android OS

Posted 17 March 2010 - 01:00 AM

Please put your code between the [ code] and [ /code] tags, thx.
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Android OS

Posted 17 March 2010 - 07:04 AM

You need to load the images into a Bitmap[] on startup. Since I too am new to Android development, I'm not sure if there is a wildcard way of doing this, but on creation, you can fill a Bitmap[] full of the needed images.

Now that you have your integer id's in an array, you can do this using a for loop:
// Make a Bitmap array
Bitmap[] images = new Bitmap[mImageIds.length];
for (int i = 0; i < mImageIds.length; i++) {
    images[i] = BitmapFactory.decodeResource(getResources(), 
         mImageIds[i]);
}



That should take care of it.

BTW, I'm moving this to "Mobile Development"

This post has been edited by Dogstopper: 17 March 2010 - 07:05 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Meetu Sinha*


Reputation:

Re: Android OS

Posted 20 March 2010 - 01:05 AM

View PostDogstopper, on 17 March 2010 - 06:04 AM, said:

You need to load the images into a Bitmap[] on startup. Since I too am new to Android development, I'm not sure if there is a wildcard way of doing this, but on creation, you can fill a Bitmap[] full of the needed images.

Now that you have your integer id's in an array, you can do this using a for loop:
// Make a Bitmap array
Bitmap[] images = new Bitmap[mImageIds.length];
for (int i = 0; i < mImageIds.length; i++) {
    images[i] = BitmapFactory.decodeResource(getResources(), 
         mImageIds[i]);
}



That should take care of it.

BTW, I'm moving this to "Mobile Development"


Hi ..
thx alot for ur reply but my question was not this... i want all my res/drawable images to be displayed in gallery nd not the ones that i have chosen/described in Integer array.this was just the code i have written nd was running fine.I need to specify some folder path wherein it will fetch all my images nd display in gallery? My concern is i dont know how and where to specify the same.Please revert asap as i urgently need it nd i am very new to android.Your help will be appreciated.Thanks in advance.
Was This Post Helpful? 0

#5 Meetu Sinha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 10

Re: Android OS

Posted 22 March 2010 - 09:19 PM

Hi

1> Please reply to my query regarding loading all my images from res/drawables into gallery ctrl without any hardcoding.
It should accept some path as a string and pick up all the images from there automatically.So that in future if i add more pictures in that folder, i do not have to make changes in my code.Path will take care of it.

2> secondly, if a click any image in agallery it should show up in original size in a different intent.i mean, opening the clicked image in a original size in a separate form/activity.Is it possible and how?

3> Third concern is how can i display the image's size and name attributes below the image itself? Is their any way through which i can retrieve the image's full details from the folder while loading the same into gallery?

I would appreciate your help if you reply to all my queries asap..

Thanks in advance...!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1