小编典典

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

java

当我单击a时,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);
    }
}

阅读 194

收藏
2020-11-30

共1个答案

小编典典

您的主要活动

 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);
2020-11-30