Android API for detecting new media from inbuilt camera & mic

后端 未结 2 1376
清歌不尽
清歌不尽 2020-12-09 16:43

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

相关标签:
2条回答
  • 2020-12-09 17:11

    Aha!

    A content observer is what i need!

    Here's where i found out about it

    0 讨论(0)
  • 2020-12-09 17:30

    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.

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