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

前端 未结 4 1232
半阙折子戏
半阙折子戏 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条回答
  • 2020-12-06 21:33

    get the path of the image that you stored and pass it as an extra using intents to other activity in the onActivityResult(). Hope this may help you.

    0 讨论(0)
  • 2020-12-06 21:42

    use startActivityForResult() instead of startActivity()

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        Bitmap thumbnail = null;
        if (requestCode == CAMERA_PIC_REQUEST) {
            if (resultCode == RESULT_OK) {
                thumbnail = (Bitmap) data.getExtras().get("data");
                Intent i = new Intent(this, NextActivity.class);
                i.putExtra("name", thumbnail);
                startActivity(i);
            }
        }
    }
    

    Next in the next activity try to use this

    protected void onCreate(Bundle savedInstanceState) {
        //TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        //intialize the image view 
    
        Bitmap bitmap  = getIntent().getExtras().getParcelable("name");
        //set the image here.
    }
    

    Hope this may help you

    0 讨论(0)
  • 2020-12-06 21:46
    public void onClick(View v) 
        {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                    
        mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                                           "image" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
        try 
            {                       
                startActivityForResult(intent, PICK_FROM_CAMERA);
            } 
        catch (ActivityNotFoundException e) 
            {
                e.printStackTrace();
            }
        }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        {
            if (resultCode != RESULT_OK) return;       
            switch (requestCode) 
                {
                    case PICK_FROM_CAMERA:
                        Bitmap photo =  null;           
                        Bundle extras = data.getExtras();
                        if (extras != null) 
                        {               
                            photo = extras.getParcelable("data");
                        }   
                        Intent i =  new Intent(this,SecondActivity.class);
                        i.putExtra("image", photo);
                        startActivity(i);
                        break;  
        }
    

    In the second activity:

        bMap =  getIntent().getParcelableExtra("image");
        mImageView.setImageBitmap(bMap);
    
    0 讨论(0)
  • 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:

    <uses-feature android:name="android.hardware.camera" />
    

    I hope this will work fine

    0 讨论(0)
提交回复
热议问题