简体   繁体   中英

Android Camera storage in SD Card

I am making an application in which user clicks a photo from the device camera and then set this image into an image view in next activity.

What happens is when I click on the camera button in my first activity the device camera's get opens and then user clciks the capture button and then it gets set in the imageview in next activity. here is my part of code

   public void loadImagefromGallery(View view) {
    try {
       // photo = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/DCIM", photoid + ".jpg");                                                           //image gets stored in DCIM folder in device's inernal memory
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
        imageUri = Uri.fromFile(photo);
        startActivityForResult(intent, TAKE_PICTURE);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

this is my onActivityResult method

   @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                try {
                    Intent intent = new Intent(this,                websters.photobooth.ImageDisplay.class);                          //sending this image to next activity
                    intent.putExtra("picture", photoid + ".jpg");
                    startActivity(intent);
                } catch (Exception e) {
                    Toast.makeText(this, e + "Failed to load", Toast.LENGTH_SHORT)
                            .show();
                    Log.e("Camera", e.toString());
                }
            }}
}

Now what I want is when Take picture button is clicked,then Camera gets opened and then after 3 secs image automatically gets clicked without any user interaction. Also for now all the images are stored in the Internal storage I want is to store them in the memory Card in some particular folder named "photobooth". Help me out guys.

if you want to capture image without user intraction then please refer this sample code which one is provided by google https://github.com/googlesamples/android-Camera2Basic and simple use CountDownTimer class here may have provide some code for your refrence how you can captcher picture.

    private void openCamera(int width, int height) {
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            requestCameraPermission();
            return;
        }
        setUpCameraOutputs(width, height);
        configureTransform(width, height);
        Activity activity = getActivity();
        CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
        try {
            if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
                throw new RuntimeException("Time out waiting to lock camera opening.");
            }
            manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
        }
    }


    private void takePicture() {
        lockFocus();
    }

    /**
     * Lock the focus as the first step for a still image capture.
     */
    private void lockFocus() {
        try {
            // This is how to tell the camera to lock focus.
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
                    CameraMetadata.CONTROL_AF_TRIGGER_START);
            // Tell #mCaptureCallback to wait for the lock.
            mState = STATE_WAITING_LOCK;
            mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                    mBackgroundHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         openCamera(your_required_width, your_required_height)
     }

     public void onFinish() {
         takePicture()
     }
  }.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM