Android API for detecting new media from inbuilt camera & mic

独自空忆成欢 提交于 2019-11-27 02:37:34

问题


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.


回答1:


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.




回答2:


Aha!

A content observer is what i need!

Here's where i found out about it



来源:https://stackoverflow.com/questions/230643/android-api-for-detecting-new-media-from-inbuilt-camera-mic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!