Skip to main content

Passing Images between Activities in Android

in First Activity:
    Intent intent=new Intent(FirstClass.this, SecondClass.class);
    Bundle bundle=new Bundle();
    bundle.putInt("image",R.drawable.ic_launcher);
    intent.putExtras(bundle);
    startActivity(intent);
in Second Acticity:
    Bundle bundle=this.getIntent().getExtras();
    int pic=bundle.getInt("image");
    v.setImageResource(pic);
another method:
in First Activity:
    Drawable drawable=imgv.getDrawable();
    Bitmap bitmap= ((BitmapDrawable)drawable).getBitmap();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] b = baos.toByteArray();
    Intent intent=new Intent(Passimage.this,myclass.class);
    intent.putExtra("picture", b);
    startActivity(intent);
in Second Acticity:
   Bundle extras = getIntent().getExtras();
   byte[] b = extras.getByteArray("picture");
   Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
   ImageView image = (ImageView) findViewById(R.id.imageView1);
   image.setImageBitmap(bmp); 

Comments

  1. This code can't working........

    ReplyDelete
    Replies
    1. Make sure that you are sending an image..The code works for me..

      Delete
  2. wooooooooooooooooooooooooooooooooooooooooooooorking!!!!!!!!!!!!!!!!!!!!!!!!!

    ReplyDelete
  3. thanks very much is working ..................................................!

    ReplyDelete
  4. thanks very much is working....!

    ReplyDelete

Post a Comment