用于从内置摄像头检测新媒体的 Android API &麦克风

编程入门 行业动态 更新时间:2024-10-25 16:20:45
本文介绍了用于从内置摄像头检测新媒体的 Android API &麦克风的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 16:10:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/942757.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:麦克风   内置摄像头   媒体   Android   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!