Background
When the user downloads a new image or captures one using the camera, the gallery app will get updated to show the new images.
I need to be notified of each new image as soon as it was created, no matter how it was created (camera, browser,...) , just as the gallery app shows.
The problem
As it turns out there is a mediaScanner Android component that is responsible for scanning all types of media files, and when it finishes, it's supposed to send an intent "MEDIA_SCANNER_FINISHED" (as shown on this example) .
So I've added the next code , hoping it will show a toast each time the user takes a photo from the camera app:
manifest:
...
<receiver android:name="com.example.newgalleryimagereceivertest.MediaScannerBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_SCANNER_FINISHED" />
<data android:scheme="file" />
</intent-filter>
</receiver>
...
java file:
package com.example.newgalleryimagereceivertest;
...
public class MediaScannerBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
android.util.Log.d("AppLog", "gallery has new images");
}
For some reason , this code doesn't work ...
The question
What's wrong with the code?
Will the broadcastReceiver ever be called after taking a photo?
What is the correct way to do it?
Do I really need to use a contentObserver and monitor its changes (using something like this), and keep the app running just for this? I really hope not...
ACTION_MEDIA_SCANNER_FINISHED is used for adding new directories to the gallery:
Broadcast Action: The media scanner has finished scanning a directory.
The path to the scanned directory is contained in the Intent.mData
field.
Constant Value: "android.intent.action.MEDIA_SCANNER_FINISHED"
You should use ACTION_MEDIA_SCANNER_SCAN_FILE, which is used to add new files - not just directories:
Broadcast Action: Request the media scanner to scan a file and add it
to the media database. The path to the file is contained in the
Intent.mData field.
Constant Value: "android.intent.action.MEDIA_SCANNER_SCAN_FILE"
You can test this Intent
using the code discussed here.
Additionally, some have reportedly needed to include mime type and file extensions, such as:
<data android:mimeType="*/*"/>
<data android:pathPattern=".*\\.png" />
You may want to look into adding these to your filter as well:
com.android.camera.NEW_PICTURE
com.android.camera.NEW_VIDEO
android.hardware.action.NEW_PICTURE
android.hardware.action.NEW_VIDEO
The android.hardware.x actions are the "official" current broadcasts, but the com.android.x ones are sent by Samsung devices and older versions of Android.
i think you can acheive this using the FileObserver .
as mentioned in the documentation :
Monitors files to fire an event after files are accessed or changed by by any process on the device .
Each FileObserver instance monitors a single file or directory. If a directory is monitored, events will be triggered for all files and subdirectories inside the monitored directory.
but you have to keep in mind :
If a FileObserver is garbage collected, it will stop sending events. To ensure you keep receiving events, you must keep a reference to the FileObserver instance from some other live object.
Please give me some feedback .
Hope That helps .