Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send image from one activity to another in android?

I am having a imageView in one class and on clicking the imageView a dialog box appear which has two option to take a image from camera or open the image gallery of device. I want to send image from one class to another so it can appear in ImageView. I am searching from many hour but i got only about sending text data from one class to another.Can any one tell about sending an image from one class to another?

This is code from sender class which will take image.

   takeImg.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if(event.getAction() == event.ACTION_UP)
                {
                    i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(i,cameraData);
                }
                return true;
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK)
        {
            Bundle extras=data.getExtras();
            bmp=(Bitmap)extras.get("data");
        }
    }

For any help thanks

like image 820
Tasneem Avatar asked Jul 22 '12 08:07

Tasneem


People also ask

How do I transfer data from one activity to another activity?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.


2 Answers

You get Image in your Activity as a Bitmap and you also pass that to another Activity as Bitmap with Intent.putExtra() like this:

First Activity.

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("bmp_Image", bmp); 

and get from second Activity like:

Bitmap bmp = (Bitmap) intent.getParcelableExtra("bmp_Image"); 

you don't need to get url and load from url.

that is the simplest way to pass the captured image from one Activity to another Activity.

like image 116
Mr.Sandy Avatar answered Sep 21 '22 14:09

Mr.Sandy


I remember something about that there is a limitation in size for putExtra() and getExtra() about 1mb. So a picture may exceed this limitation. How about just passing the path to the picture?

like image 22
sschrass Avatar answered Sep 23 '22 14:09

sschrass