Android new file on sdcard Broadcast event - android

I want to be notified (via broadcast) when a new file is created on the sdcard in a particular folder.
I can create a service and poll for it, but I would prefer an system event if it exists.

I found it. FileObserver would do it.

Related

Broadcast when user add new file to SDcard

In my cloud backup application i want to upload any image in sdcard immediately create it -
in other words - when user has a new (image) file in his sdcard, (for example, download from whatsapp/email/ added from PC by USB) run a function in my application..
I thought about Broadcast, there is any broadcast who can do it? another way?
You can use android.os.FileObserver:
http://developer.android.com/reference/android/os/FileObserver.html

Trigger the rescan of MediaStore programmatically

Notice that the newly added photos(copied over using "adb push" into Pictures folder) are not included in the MediaStore Content Provider. Is there a way to trigger the rescan programmatically?
Already tried:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse(Environment.getExternalStorageDirectory().getPath())));
and
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/Pictures")));
which seem not work.
The broadcast Intent.ACTION_MEDIA_MOUNTED cannot be used after Android 4.4.
(1) Other than using MediaScannerWrapper to scan files myself, is there other way to programmatically trigger the rescan?
(2) Under what circumstances will the rescan happen in Android?
Already tried
You are passing a directory, not a file.
is there other way to programatically trigger the rescan?
Use MediaScannerConnection and its scanFile() method. This too requires a file AFAIK.

Not able to access a file on reboot in Android

I am developing an app where I load my settings of the app by reading an xml file stored in the external storage. But whenever I reboot the system, I am not able to access the xml for the first time, my app crashes because of this and restarts. from then it can access the xml file. Any clue on whats happening, or what I am doing wrong?
My sample code to open the file:
filepath= Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(filepath+"/xy.xml");
Please check if the External Storage is available. Use a broadcast receiver, listen for Intent.ACTION_MEDIA_MOUNTED : Broadcast Action: External media is present and mounted at its mount point.

DownloadManager runs MediaScanner after download but shouldn't

it seems that MediaScanner wants to scan files that I told it not to. Now I wonder why.
My app downloads several media files from my server and shows them later with a playlist.
For that, the app gets the media files with the Android system's DownloadManager.
Using Request.setDestinationUri(), the download will be saved to a subdirectory of getExternalCacheDir() named "pending".
When the download is finished, the Android DownloadManager sends ACTION_DOWNLOAD_COMPLETE broadcast. My app's broadcast listener will then take that finished download and move it from the "pending" folder to a different folder named "media".
All this works as intended.
However, the system log is full of messages like these:
E/BitmapFactory(23779): Unable to decode stream: java.io.FileNotFoundException: /path/to/pending/image.jpg: open failed: ENOENT (No such file or directory)
E/JHEAD (23779): can't open '/path/to/pending/image.jpg'
E/StagefrightMetadataRetriever(25911): Unable to create data source for '/path/to/pending/video.mp4'.
E/MediaScannerJNI(23779): An error occurred while scanning file '/path/to/pending/video.mp4'.
So apparently, my app tells the DownloadManager to download an image / a video to the "pending" directory. It does as it's told and sends a "I completed the download" broadcast. My app receives the broadcast and moves the complete file to the "media" directory. Some moments later, the MediaScanner (or something else) tries to scan the completed file in the "pending" folder and barfs into the system log.
Now I'm wondering: Why is MediaScanner trying to read these files, anyway?
According to the Android API doc for setDestinationUri: "The downloaded file is not scanned by MediaScanner. But it can be made scannable by calling allowScanningByMediaScanner()." I don't call that method, so the downloaded file should not be scanned.
Next, I tried to put an empty .ignore in the app's cache directory and reminded the MediaScanner of the .ignore-file's existence through ACTION_MEDIA_SCANNER_SCAN_FILE, but the error messages remain.
To add to the mystique, the files do not show up in the system's gallery or video apps, so yes, the media scanner ignores them. But still: Why does it try to read them when it doesn't have to? Is it the MediaScanner at all or is it some other system service?
Next, I tried to put an empty .ignore in the app's cache directory and reminded the MediaScanner of the .ignore-file's existence through ACTION_MEDIA_SCANNER_SCAN_FILE, but the error messages remain.
The magic file to stop the Mediascanner is not ".ignore" but ".nomedia".
From MediaScanner.java:
File file = new File(path.substring(0, slashIndex) + ".nomedia");
if (file.exists()) {
// we have a .nomedia in one of the parent directories
return true;
}
And the reason why it did not appear in the system's gallery or video apps is maybe, because the scan crashed (as indicated in the log).
However, I have bad feelings about the media scanner too. For example, why doesn't it stop scanning in a more straight way. For example, in MediaScanner.java, instead of
public void scanDirectories(String[] directories, String volumeName) {
[...]
for (int i = 0; i < directories.length; i++) {
processDirectory(directories[i], mClient);
}
it could be
public void scanDirectories(String[] directories, String volumeName) {
[...]
for (int i = 0; i < directories.length; i++) {
if (! isNoMediaPath(directories[i]))
processDirectory(directories[i], mClient);
}
But instead, it goes forth and back between java code and cpp code again and again. What is the reason for that?
It really looks like the ".nomedia" won't give even half the effect some might expect. After further investigating in MediaScanner.java I would say, that this file does not stop the MediaScanner from scanning the whole tree at all. It still adds entries to MediaStore for each file, never mind of noMedia being set or not.
It just marks those entries in the MediaStore as "must not show up". On each file it does an "beginFile" and an "endFile". In the endFile it always does a mMediaInserter.insert, the one way or the other.
What bothers me so much about this is the fact, that it scans through all the files in e.g. a mounted stick, hereby taking the risk of trapping into a virus (specially designed for that scan process) and no .nomedia file can stop it from doing so.

Android : Monitoring folder activity

I want to create an app which would monitor changes to data in a folder on SD card.
For example if a file is put in a folder, as soon as file is copied, my app would send this file to server and delete this file.
Is it possible to do this?
Thanks.
I think you can use android.os.FileObserver which is available since API Level 1.
Source: http://developer.android.com/reference/android/os/FileObserver.html
I looks like there is no system-wide "FileSystemMonitor" you could connect to.
So you have to write it yourself. You could for example receive the ACTION_TIME_TICK broadcast and check the filesystem for changes yourself every minute using the normal java File classes.

Categories

Resources