如何在添加新视频文件时收到通知

时间:2015-03-12 05:21:47

标签: java android broadcastreceiver contentobserver fileobserver

如何检测相机已捕获新视频。我已经更新了我的代码

更新代码:

public class Video extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        // TODO Auto-generated method stub

        Log.d("SCAN_FILE", "VIDEOOOOOOOOOOOOOOOOOOO");

        String action = intent.getAction();

        Uri uri = intent.getData();

        String externalStoragePath = Environment.getExternalStorageDirectory().getPath();

        if (uri.getScheme().equals("file")) 
        {
            String path = uri.getPath();

            if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) &&
                    path != null && path.startsWith(externalStoragePath + "/")) 
            {
                String newFileURL = intent.getDataString();

                scanFile(context, path, newFileURL);
            }
        }
    }

     private void scanFile(Context context, String path, String URL) 
     {
         Log.d("SCAN_FILE", path);

         Log.d("SCAN_URL", URL);
     }                      
}

清单:

<receiver android:name=".Video" >
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_SCANNER_SCAN_FILE" />
            </intent-filter>
        </receiver>

等待您的专家意见。

更新:此广播没有起火

1 个答案:

答案 0 :(得分:2)

更新:在模拟器和真实设备上进行了测试。

您需要创建在Camera目录中侦听新文件的服务。

<强> MediaListenerService.java

import android.app.Service;
import android.content.Intent;
import android.os.FileObserver;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
import java.io.File;

public class MediaListenerService extends Service {

    public static FileObserver observer;

    public MediaListenerService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        startWatching();
    }

    private void startWatching() {
        final String pathToWatch = android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/";
        Toast.makeText(this, "My Service Started and trying to watch " + pathToWatch, Toast.LENGTH_LONG).show();

        observer = new FileObserver(pathToWatch, FileObserver.CLOSE_WRITE) { // set up a file observer to watch this directory on sd card
            @Override
            public void onEvent(int event, final String file) {
                if (!file.equals(".probe")) { // check that it's not equal to .probe because thats created every time camera is launched
                    Log.d("MediaListenerService", "File created [" + pathToWatch + file + "]");

                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getBaseContext(), file + " was saved!", Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }
        };
        observer.startWatching();
    }
}

AndroidManifest.xml标记内的<application>内声明服务

<service
    android:name=".service.MediaListenerService"
    android:enabled="true"
    android:exported="false" >
</service>

也不要忘记添加权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

现在从您的活动中启动服务。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startService(new Intent(getBaseContext(), MediaListenerService.class));
}

如果您想在启动时启动服务,只需创建一个收听android.intent.action.BOOT_COMPLETED的接收器,然后从中启动服务。

希望这有帮助。