用于从内置摄像头检测新媒体的 Android API &麦克风 [英] Android API for detecting new media from inbuilt camera & mic

查看:25
本文介绍了用于从内置摄像头检测新媒体的 Android API &麦克风的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android API 中是否有任何优雅的方式来检测写入设备时的新媒体?我主要对相机拍摄的照片、相机拍摄的视频和麦克风录制的音频感兴趣.

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.

推荐答案

有一个特殊的广播 Intent,每次应用程序向 Media Store 写入任何新内容时都应该调用它:

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

广播意图包含新文件的路径,可通过 Intent.getDataString() 方法访问.

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

要监听它,只需创建一个 BroadcastReceiver 并使用 IntentFilter 注册它,如下所示:

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));

这仅适用于插入媒体商店内容提供商之一的文件.此外,这取决于将其放在那里广播意图的应用程序,所有本机 (Google) 应用程序都会这样做.

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.

这篇关于用于从内置摄像头检测新媒体的 Android API &麦克风的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆