如何将捕获的图像显示到新的活动上? [英] How to show captured Image onto new Activity?

查看:60
本文介绍了如何将捕获的图像显示到新的活动上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击一个 Button 时,它会打开相机,并在保存时将其保存到图库中.我想在新活动中将该图片显示为 ImageView .

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

推荐答案

您的主要活动

 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方法上编写此代码

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

这篇关于如何将捕获的图像显示到新的活动上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆