Get image from capture and show image in another layout using another activity in android

前端 未结 4 1231
半阙折子戏
半阙折子戏 2020-12-06 21:23

I want to show image after capture by click button Capture in the FirstActivity and show image in the activity_second(layout) using SecondActivity.

FirstActivity

4条回答
  •  -上瘾入骨i
    2020-12-06 21:52

    You can use the following code to solve the issues :

    Your First Activity:

    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_first);
            Button take_photo = (Button) findViewById(R.id.btn_capture);
            take_photo.setOnClickListener(new OnClickListener() 
            {
                public void onClick(View v) 
                {
                    // create Intent to take a picture and return control to the calling application
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    // start the image capture Intent
                    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
                }
            });
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        {
            if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) 
            {
                Bitmap imageData = null;
                if (resultCode == RESULT_OK) 
                {
                    imageData = (Bitmap) data.getExtras().get("data");
    
                    Intent i = new Intent(this, SecondActivity.class);
                    i.putExtra("name", imageData );
                    startActivity(i);
    
                } 
                else if (resultCode == RESULT_CANCELED) 
                {
                    // User cancelled the image capture
                } 
                else 
                {
                    // Image capture failed, advise user
                }
            }
        }
    

    and second Activity:

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
            Bitmap bitmap  = getIntent().getExtras().getParcelable("name");
            ImageView view = (ImageView) findViewById(R.id.view_photo);
            view.setImageBitmap(bitmap); 
        }
    

    and in Manifest.xml file use the permission:

    
    

    I hope this will work fine

提交回复
热议问题