Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android API for detecting new media from inbuilt camera & mic

Is there any elegant way in the Android API for detecting new media when it is written to the device? I’m mainly interested in photos taken by the camera, video taken by the camera and audio recorded from the mic.

My current thinking is to periodically scan each media content provider and filter based on last scan time.

I’m just wondering if there is some service I can get realtime notifications.

like image 645
Declan Shanaghy Avatar asked Oct 23 '08 17:10

Declan Shanaghy


People also ask

What is multi camera API?

The multi-camera API. Multiple streams simultaneously. Creating a session with multiple physical cameras. Using a pair of physical cameras.

What is Media API in Android?

The Media APIs are used to play and, in some cases, record media files. This includes audio (e.g., play MP3s or other music files, ringtones, game sound effects, or DTMF tones) and video (e.g., play a video streamed over the web or from local storage).

What is camera API?

This package is the primary API for controlling device cameras. It can be used to take pictures or videos when you are building a camera application. Camera. This class is the older deprecated API for controlling device cameras.

How can we launch an existing camera application in the Android app?

Modify src/MainActivity. java file to add intent code to launch the Camera. Add the Camera permission and run the application and choose a running android device and install the application on it and verify the results. Following is the content of the modified main activity file src/MainActivity.


1 Answers

There's a special broadcast Intent that should get called every time an application writes anything new to the Media Store:

Intent.ACTION_MEDIA_SCANNER_SCAN_FILE

The Broadcast Intent includes the path to the new file, accessible through the Intent.getDataString() method.

To listen for it, just create a BroadcastReceiver and register it using an IntentFilter as shown below:

registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      String newFileURL = intent.getDataString();
      // TODO React to new Media here.  
    }    
  }, new IntentFilter(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE));

This will only work for files being inserted into one of the Media Store Content Providers. Also, it depends on the application that's putting it there broadcasting the intent, which all the native (Google) application do.

like image 105
Reto Meier Avatar answered Oct 16 '22 03:10

Reto Meier