android video app updating media store

The android video app updating media store tutorial describes how to notify the MediaStore when the app has captured a new image or video.

This is important for other applications such as the mediastore viewer to be able to load and display the most recent capture image or video.

Get Code

The code to start this tutorial is on github here

https://github.com/mobapptuts/android_camera2_api_video_app.git Tag camera2-video-scanner

or you can run this command

git clone https://github.com/mobapptuts/android_camera2_api_video_app.git –branch camera2-video-scanner

Link to youtube tutorial describing how to do this using android studio

Steps

Call the media scanner intent once the image has been saved

private class ImageSaver implements Runnable {

     private final Image mImage;

     public ImageSaver(Image image) {
         mImage = image;
     }

     @Override
     public void run() {
         ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer();
         byte[] bytes = new byte[byteBuffer.remaining()];
         byteBuffer.get(bytes);

         FileOutputStream fileOutputStream = null;
         try {
             fileOutputStream = new FileOutputStream(mImageFileName);
             fileOutputStream.write(bytes);
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             mImage.close();

             Intent mediaStoreUpdateIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
             mediaStoreUpdateIntent.setData(Uri.fromFile(new File(mImageFileName)));
             sendBroadcast(mediaStoreUpdateIntent);

             if(fileOutputStream != null) {
                 try {
                     fileOutputStream.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }

     }
 }

 

 

 Call the media store scanner intent once the video recording has stopped

@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_camera2_video_image);

      createVideoFolder();
      createImageFolder();

      mChronometer = (Chronometer) findViewById(R.id.chronometer);
      mTextureView = (TextureView) findViewById(R.id.textureView);
      mStillImageButton = (ImageButton) findViewById(R.id.cameraImageButton2);
      mStillImageButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              checkWriteStoragePermission();
              lockFocus();
          }
      });
      mRecordImageButton = (ImageButton) findViewById(R.id.videoOnlineImageButton);
      mRecordImageButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (mIsRecording || mIsTimelapse) {
                  mChronometer.stop();
                  mChronometer.setVisibility(View.INVISIBLE);
                  mIsRecording = false;
                  mIsTimelapse = false;
                  mRecordImageButton.setImageResource(R.mipmap.btn_video_online);
                  mMediaRecorder.stop();
                  mMediaRecorder.reset();

                  Intent mediaStoreUpdateIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                  mediaStoreUpdateIntent.setData(Uri.fromFile(new File(mVideoFileName)));
                  sendBroadcast(mediaStoreUpdateIntent);

                  startPreview();
              } else {
                  mIsRecording = true;
                  mRecordImageButton.setImageResource(R.mipmap.btn_video_busy);
                  checkWriteStoragePermission();
              }
          }
      });
      mRecordImageButton.setOnLongClickListener(new View.OnLongClickListener() {
          @Override
          public boolean onLongClick(View v) {
              mIsTimelapse =true;
              mRecordImageButton.setImageResource(R.mipmap.btn_timelapse);
              checkWriteStoragePermission();
              return true;
          }
      });
  }

 

 

Android video app updating media store summary

In the android video app time lapse tutorial we described the steps for how to notify the MediaStore for when the video app has captured a video or image.

Applications such as the mediastore viewer depend on the Camera2 Video app to provide the notification to the mediastore before they themselves can display the most recent captured video or image.

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>