如何将捕获的图像显示到新活动?

时间:2017-08-08 04:15:58

标签: java android

当我点击Button时,它会打开相机,当我保存时,它会保存到图库中。我希望将该图片作为ImageView显示在新活动中。

这是我点击相机按钮的代码

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);
    }
}

2 个答案:

答案 0 :(得分:3)

您的主要活动

 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);
        }
    }
您的Activity2上的

在onCreate方法上编写此代码

Bundle ex = getIntent().getExtras();
Bitmap bmp2 = ex.getParceable("image");
ImageView result = (ImageView)findViewById(R.Id.imageView1);
result.setImageBitmap(bmp);

答案 1 :(得分:0)

使用startActivityForResult()触发Image上传屏幕并使用onActivityResult()获取image数据,并将其显示在ImageView

参考this