1

I have 2 activities.In first activity there is a button called take photo when I clicked on it my app uses a camera intent of android phone.But I want the captured image in the image view of 2nd activity how can I do it?

Here bt1 is the button in MainActivity_Lens. cameraActivity is the secondactivity.

  public class MainActivity_Lens extends AppCompatActivity {

  Button bt1;
    public static final int req=1;


    public void init(){
        bt1=(Button)findViewById(R.id.takephoto);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent camera=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(camera,req);

//                Intent cameraview =new Intent(MainActivity_Lens.this,cameraActivity.class);
//                startActivity(cameraview);
            }
            protected void onActivityResult(int requestcode,int resultcode,Intent data){
                if(requestcode==req && resultcode==RESULT_OK){
                    Bundle extras=data.getExtras();
                    Bitmap photoCapturedBitmap =(Bitmap) extras.get("data");
                    Intent cameraview =new Intent(MainActivity_Lens.this,cameraActivity.class);
                    startActivity(cameraview);
                }
            }
        });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main_activity__lens);
        init();
    }



}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • This `Intent data` within onAcitivityResult is likely going to be null after the activity get restarted when the camera launches, you have to manage the data you want to pass but not via the intent. – Fevly Pallar Jan 15 '17 at 13:37
  • [http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android] – Gary99 Jan 15 '17 at 15:08

1 Answers1

0

to pass image Bitmap between activity use:

Declare:

byte[] bytesImage ;

convert bitmap to byte array:

ByteArrayOutputStream ba = new ByteArrayOutputStream();
//bitmap is the bitmap you received in onActivityResult()
bitmap.compress(Bitmap.CompressFormat.PNG, 50, ba);
//save the image byte array
bytesImage = ba.toByteArray();

Now pass it to other activity;

Intent i = new Intent(MainActivity_Lens.this, cameraActivity.class);
i.putExtra("byteArray", bytesImage);
startActivity(i);

Receive it in secondactivity using;

byte[] byteArray = getIntent().getByteArrayExtra("byteArray");
    if (byteArray != null) {
        Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
        imageview.setImageBitmap(bmp);//set bitmap to imageview
    }
rafaelc
  • 57,686
  • 15
  • 58
  • 82
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • byte[] bytesImage ; ByteArrayOutputStream ba = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 50, ba); bytesImage = ba.toByteArray(); Intent i = new Intent(MainActivity_Lens.this, cameraActivity.class); i.putExtra("byteArray", bytesImage); startActivity(i); // Should i include above code in OnActivity method ?//@rafsanahmad007 – sameera varma addepalli Jan 16 '17 at 07:35
  • you mean in `onactiviytResult` you can..put – rafsanahmad007 Jan 16 '17 at 07:39
  • @sameera varma addepalli did it worked? – rafsanahmad007 Jan 16 '17 at 16:33
  • @sameeravarmaaddepalli if the answer work accept it by clicking the tick button on left side of the answer – rafsanahmad007 Jan 18 '17 at 10:55