How to detect that camera has captured a new video. I have updated my code
Updated Code :
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);
}
}
Manifest :
<receiver android:name=".Video" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_SCANNER_SCAN_FILE" />
</intent-filter>
</receiver>
Waiting for your expert advice.
update : This Broadcast is not getting fire
Updated: Tested on both emulator and real device.
You need to create the service that listen for new file in Camera directory.
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();
}
}
Declare the service in AndroidManifest.xml
inside <application>
tag
<service
android:name=".service.MediaListenerService"
android:enabled="true"
android:exported="false" >
</service>
And also don't forget to add a permission:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Now start the service from your Activity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(getBaseContext(), MediaListenerService.class));
}
If you want to make your service started on boot, just simply create a receiver that listen to android.intent.action.BOOT_COMPLETED
and then launch the service from that.
Hope this helps.