0

When I click a Button it opens the camera and when I save it, it saves into the gallery. I want to display that picture in a new activity as an ImageView.

This is the code I have for the camera button click

btn2 =(Button)findViewById(R.id.drawer_two);
btn2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent upload = new Intent("android.media.action.IMAGE_CAPTURE");
        startActivity(upload);
    }
}
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53

2 Answers2

3

Your Main Activity

 public class MainActivity extends ActionBarActivity {

        private final int requestCode = 20;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            Button capturedImageButton = (Button)findViewById(R.id.photo_button);
            capturedImageButton.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(photoCaptureIntent, requestCode);
                }
            });
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(this.requestCode == requestCode && resultCode == RESULT_OK){
                Bitmap bitmap = (Bitmap)data.getExtras().get("data");
                Intent in1 = new Intent(this, Activity2.class);
                in1.putExtra("image",bitmap);
                 startActivity(in1);
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }

on Your Activity2 write this code on onCreate method

Bundle ex = getIntent().getExtras();
Bitmap bmp2 = ex.getParceable("image");
ImageView result = (ImageView)findViewById(R.Id.imageView1);
result.setImageBitmap(bmp);
Sagar Patel
  • 224
  • 1
  • 9
0

use startActivityForResult() for firing Image upload screen and use onActivityResult() to get the image data back and to display it in ImageView

Refer this

Rajan Kali
  • 12,627
  • 3
  • 25
  • 37