简体   繁体   中英

How to click image view and send that image in another activity through intent?

How to click an image view with ClickListener and how to send or pass that image to another activity through an intent

imgview.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub


        }
    });

I dont know the coding as I am new to android

Try this....

 imageview.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent i = new Intent(MainActivity.this,HomeActivity.class);
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    i.putExtra("bmp_img", bmp);
    startActivity(i);

and in your second activity put this...

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
 imageView.setImageBitmap(bitmap);

Try below code.

Write this in your FirstActivity.java

imgview.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
             Intent mIntent = new Intent(this, ActivityTwo.class);
             Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
             mIntent.putExtra("bmp_img", bmp);
             startActivity(mIntent);
        }
    });

and this is in SecondActivity.java

Take one image-view in your XML, and then pass this code in onCreate method

Bitmap mBitmap = (Bitmap) intent.getParcelableExtra("bmp_img");
imageview.setImageResource(mBitmap);

Replace this R.drawable.ic_launcher with whatever image you want to pass.

1) How to click image view

ImageView img = (ImageView) findViewById(R.id.myImageId);
img.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
       // your code here
    }
});

2) How to send that image in another activity through intent

at First Activity.

    Intent currentIntent = new Intent(MainActivity.this, ActivitySecond.class);
    currentIntent .putExtra("bmp_img", bmp);
startActivity(currentIntent );

for getting output in second activity,

in Second Activity.

Bitmap mBitmap = (Bitmap) intent.getParcelableExtra("bmp_img");

Finally you can write as follows

    ImageView img = (ImageView) findViewById(R.id.myImageId);
    img .setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                 Intent currentIntent = new Intent(MainActivity.this, ActivitySecond.class);
    //suppose your image is simple icon launcher
                 Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                 currentIntent .putExtra("bmp_img", bmp);
startActivity(currentIntent );
            }
        });

and now in second activity

 ImageView imageview= (ImageView) findViewById(R.id.yourImageId);
Bitmap mBitmap = (Bitmap) intent.getParcelableExtra("bmp_img");
imageview.setImageResource(mBitmap);
imgview.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

Intent myIntent=new Intent(ThisView.this,NextView.class);
             Bundle i = new Bundle();
            i.putByte("Image", yourImage);
            myIntent.putExtras(i);
            startActivity(myIntent);


    }
});

Use following code.

imageView.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

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

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

In Another Activity call llike below.

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

image.setImageBitmap(bmp );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM