get the id of an image selected from gallery

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

get the id of an image selected from gallery

Postby maanatiitd » Thu May 27, 2010 12:39 pm

Hi,
i am stuck in a simple problem. I have made two activities. one is Window_to_call and other is Maker. Initially my Window_to_call appears and it contains a button and a blank imageViewer. What i want is when i click on the button, window associated with maker should appear with an image gallery in it and when i click any image this activity should stop and return to the previous window and the selected image should appear on the ImageViewer of Window_to_call.

My problem:

I am using following code to send the image from Maker to Window_to_call...
////////code///////
Bitmap bm= BitmapFactory.decodeResource(getResources(),????????);
Intent intent = new Intent(Maker.this, Window_to_call.class);


intent.putExtra("bitmap", bm );
startActivity(intent);
finish();
////////code///////
now at the place of ?????? mark i need to give the resource id. how can i get it.

Earlier i have worked on different problem where i needed to send an image "one.jpg" from Activity1 to Activity2
there i used the following code
////////code//////
itmap bm= BitmapFactory.decodeResource(getResources(),R.drawable.one);
Intent intent = new Intent(Activity1.this, Activity2.class);


intent.putExtra("bitmap", bm );
startActivity(intent);
finish();
////////code//////

here i stored one.jpg in drawable and got it directly. but in Maker Activity what should i do ....
plz help me..!!!!

Regards
Manish
maanatiitd
Freshman
Freshman
 
Posts: 3
Joined: Wed May 26, 2010 2:49 pm

Top

Re: get the id of an image selected from gallery

Postby Shota » Mon May 31, 2010 7:12 am

To know the id you have to use the method getSelectedItemPosition() by Gallery.
For examle:
Code: Select all
OnItemSelectedListener Listen = new OnItemSelectedListener(){
                Button b = (Button)this.findViewById(R.id.button);
                Gallery G = (Gallery)this.findViewById(R.id.imagechooser);
      @Override
      public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
      {
         b.setText(new Integer(G.getSelectedItemPosition()).toString());   
      }

      @Override
      public void onNothingSelected(AdapterView<?> arg0) {
         // TODO Auto-generated method stub
         
      }
       
    };
   
    G.setOnItemSelectedListener(Listen);
Shota
Freshman
Freshman
 
Posts: 5
Joined: Tue May 18, 2010 6:57 pm

Re: get the id of an image selected from gallery

Postby maanatiitd » Tue Jun 01, 2010 12:46 pm

hi thanks for your reply ...but i guess i need to explain more about my problem cause the funda you gave me didn't work or i guess i applied it wrongly...
my main problem is image sending between two activities.
i am sending you the query in detail and with code.

My first Activity is "Window_to_call". It shows a button (and it also has a blank imageViewer), when the button is clicked it starts second activity "Maker". Maker shows a gallery. What i want is when i click on any image of the gallery, window of Maker should close and the clicked image should appear in the imageViewer of "Window_to_call" .
I am sending you code of both the activities.

Window_to_call.java code:
Code: Select all
package make.gallery;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Window_to_call extends Activity {
 
   
   ImageView returnpic;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
       
   
        returnpic = (ImageView) findViewById(R.id.ImageView01);
       
       
        Button next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
               Intent intent = new Intent(view.getContext(), Maker.class);
               startActivityForResult(intent, 0);
               Bitmap bm = intent.getParcelableExtra("bitmap");
               returnpic.setImageBitmap(bm);         
             }

        });
       
    }
}




Maker.java code:
Code: Select all
package make.gallery;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.content.res.Resources;

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.content.Intent;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.AdapterView.OnItemClickListener;
import android.view.View;
import android.view.ViewGroup;

public class Maker extends Activity {
   
   Gallery g;
      
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
         g = (Gallery) findViewById(R.id.gallery);
         g.setAdapter(new ImageAdapter(this));

         g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
               
               Toast.makeText(Maker.this, "" + position, Toast.LENGTH_SHORT).show();         
             
               Bitmap bm= BitmapFactory.decodeResource(getResources(),g.getSelectedItemPosition());
               Intent intent = new Intent(Maker.this, Window_to_call.class);
               intent.putExtra("bitmap", bm );
               setResult(RESULT_OK, intent);
               startActivity(intent);
               finish();
               
            }
        });
           
    }
   
     
     
   
    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

        private Integer[] mImageIds = {
               
               
                R.drawable.sample_4,
                R.drawable.sample_5,
                R.drawable.sample_6,
                R.drawable.sample_7
        };

        public ImageAdapter(Context c) {
            mContext = c;
            TypedArray a = obtainStyledAttributes(R.styleable.Maker);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.Maker_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.setImageResource(mImageIds[position]);
            i.setLayoutParams(new Gallery.LayoutParams(150, 100));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setBackgroundResource(mGalleryItemBackground);

            return i;
        }
    }
}



I don't know where i am committing mistakes and whether my strategy is wrong for sending images.
plz help me...i have been fighting with this problem for more than a week.
maanatiitd
Freshman
Freshman
 
Posts: 3
Joined: Wed May 26, 2010 2:49 pm

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 4 guests