android camera2 api update gallery

android camera2 api refresh image gallery

This episode of the android camera2 api tutorial series implements the refresh of the grid gallery of an image has been captured.

In this tutorial we focus on implementing the android camera2 api update gallery so that the latest captured images will be displayed in the gallery.

This is slightly complicated for CameraCaptureSession callback is currently being executed in the background thread handler therefore making the RecyclerView and Adapter members not accessible.

The quick solution to this is to create a handler from the main ui thread and then to add that handler to CameraCaptureSession capture method therefore ensuring the callback is called from within the context of the main ui thread making the RecyclerView & Adapter members available.

Get Code

You can get the code from github here

https://github.com/mobapptuts/recyclerview_image_gallery.git Tag camera2-update-gallery

or else run this command

git clone –branch camera2-update-gallery https://github.com/mobapptuts/recyclerview_image_gallery.git

Code Samples

Create a method for swapping the ImageAdapter

private void swapImageAdapter() {
      RecyclerView.Adapter newImageAdapter = new ImageAdapter(sortFilesToLatest(mGalleryFolder));
      mRecyclerView.swapAdapter(newImageAdapter, false);
  }

Create the handler for the main ui thread and then pass it to the capture method. Call the swapImageAdapter from within the callback.

private void captureStillImage() {

      Handler uiHandler = new Handler(getMainLooper());

      try {
          CaptureRequest.Builder captureStillBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
          captureStillBuilder.addTarget(mImageReader.getSurface());

          int rotation = getWindowManager().getDefaultDisplay().getRotation();
          captureStillBuilder.set(CaptureRequest.JPEG_ORIENTATION,
                  ORIENTATIONS.get(rotation));

          CameraCaptureSession.CaptureCallback captureCallback =
                  new CameraCaptureSession.CaptureCallback() {
                      @Override
                      public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                          super.onCaptureCompleted(session, request, result);

                          swapImageAdapter();
                          /*
                          Toast.makeText(getApplicationContext(),
                                  "Image Captured!", Toast.LENGTH_SHORT).show();
                          */
                          unLockFocus();
                      }
                  };

          mCameraCaptureSession.capture(
                  captureStillBuilder.build(), captureCallback, uiHandler
          );

      } catch (CameraAccessException e) {
          e.printStackTrace();
      }
  }
About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>