Arif YILMAZ
Arif YILMAZ

Reputation: 5876

get ImageView's image and send it to an activity with Intent

I have a grid of many products in my app. when the user selects one of the item in the grid, I am starting a new activity as DIALOG box and display the item's name,quantity and image. But I cannot send the image source dynamically.

here is my code

gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
            //Toast.makeText(getApplicationContext(),((TextView) v.findViewById(R.id.grid_item_label)).getText(), Toast.LENGTH_SHORT).show();
            Intent item_intent = new Intent(MainActivity.this, Item.class);
            item_intent.putExtra("name",((TextView) v.findViewById(R.id.grid_item_label)).getText());
            item_intent.putExtra("quantity",((TextView) v.findViewById(R.id.grid_item_quantity)).getText());

            //my problem is here***************************************************
            ImageView my_image =  findViewById(R.id.grid_item_image).getDrawable();
            item_intent.putExtra("image",my_image.getXXXXX());
            //*********************************************************************
            MainActivity.this.startActivity(item_intent);

        }
    });

What should I use to get the image source from ImageView?

Upvotes: 4

Views: 58516

Answers (6)

Javad Karbasian
Javad Karbasian

Reputation: 93

i had the same problem and solve it with passing the position of selected item via intent. as i think and read from professionals in stack its the best solution. just do that in FirstActivity.java that Contains GridView

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Intent intent = new Intent(getActivity(),DetailActivity.class);
    intent.putExtra("id",position);
     startActivity(intent);    } });

and then in your destination Activity you must get the position from extras and extract it from your database or where your images array are cast:

Intent intent = getActivity().getIntent();
            int position = intent.getIntExtra("id",0);
            ImageAdapter imageAdapter = new ImageAdapter(getActivity());
            ImageView imageView = (ImageView)rootView.findViewById(R.id.movie_detail_image);
            imageView.setImageResource((int)imageAdapter.getItem(position));

you must edit your image adapter getItem method to return selected image id.

Upvotes: 0

Bram
Bram

Reputation: 4613

You can put Parcable (bitmap for example) or serializable objects in a bundle and retrieve the object from the Bundle in the other activity.

how do you pass images (bitmaps) between android activities using bundles?

Although I would not recommend it because you pass a lot of data between activities. I would recommend you to pass the image reference id to the next activity and retrieve the image there.

How to pass the selectedListItem's object to another activity?

EDIT:

intent.putExtra("imageId", imageId);

in the other activity:

int imageRef = getIntent().getIntExtra("imageId", defaultValue);

http://developer.android.com/reference/android/content/Intent.html#getIntExtra%28java.lang.String,%20int%29

Upvotes: 2

Bald bcs of IT
Bald bcs of IT

Reputation: 892

Use Bundle like

imageView.buildDrawingCache();
Bitmap image= imageView.getDrawingCache();

 Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intent.putExtras(extras);
startActivity(intent);


Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");

image.setImageBitmap(bmp );

Upvotes: 35

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28399

When passing around Bitmap data between Activities, I generally prefer to store it in an extended Application class, which you have access to from all your Activities. You can obviously do it as the others have said, by passing it on the Intent, but if you need it in more than a couple of Activities, storing it in the Application gives you more flexibility.

Upvotes: 1

Kosh
Kosh

Reputation: 6334

first you'll have to save your image first and then you can use this method to send the file name of your saved image

    void Send() { 
            if (file != null) {
            Intent intent = new Intent(this, Draw.class);
            intent.putExtra(PICTURE_FILE_ID, file);
            startActivity(intent);
        } else if (file == null) {
            Toast tst = Toast.makeText(getApplication(),
                    "Please Click Save First", Toast.LENGTH_SHORT);
            tst.setGravity(Gravity.CENTER, 0, 0);
            tst.show();
        }
}

the other activity

use mBitmapFile = (File) getIntent().getSerializableExtra( YourClassName.PICTURE_FILE_ID);

Upvotes: 2

Geralt_Encore
Geralt_Encore

Reputation: 3771

You need to convert drawable into Bitmap using this solution: And then you can pass it to the next activity via intent, because Bitmap class implements Parcelable interface.

Upvotes: 2

Related Questions