FlamingMoe
FlamingMoe

Reputation: 2992

How to create an Android listener to capture all incoming/outgoing images/video from Whatsapp or others?

I've seen some photo hosting apps that "listen" in some way to Whatsapp, and every time I receive or send a picture in my Whatsapp, they take it and upload it to their hosting.

How can I listen to that folder in photo directory and get those images ?

I don't undertand how to build an intent "listening" to other folders

Besides, in my case (with Whatsapp) , the folder can be in different places depending on the mobile and if it has SD card or not ... how to know the whatsapp folder in the mobile and then create an intent to get all the pictures as they are coming in real time ?

Upvotes: 3

Views: 2914

Answers (3)

Manuel Allenspach
Manuel Allenspach

Reputation: 12745

I suggest you to use a FileObserver and let it run on the WhatsApp directories:

String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
FileObserver observer = createFileObserver(sdcardPath + "/WhatsApp/Media/WhatsApp Images/");
observer.startWatching();
...
observer.stopWatching();

I created a method here to reuse the code if you want to run multiple observers with different paths. You could also create a class that extends FileObserver.

private FileObserver createFileObserver(String path) {
    FileObserver observer = new FileObserver(path) {    
        @Override
        public void onEvent(int event, String file) {
            if(event == FileObserver.CREATE){
                Toast.makeText(getApplicationContext(), file + " was created!", Toast.LENGTH_SHORT).show();
            } //...
        }
    };
    return observer;
}

Edit: WhatsApp does not seem to work on phones without sd cards. It shows the error Cannot download because no SD card is available. Please insert your SD card or unmount it as a disk drive, and try again.

Upvotes: 1

VVB
VVB

Reputation: 7641

Why dont yout try for FileObserver. Here I'm providing you link for it.

File Observer is,

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.

http://developer.android.com/reference/android/os/FileObserver.html

I hope this will help you.

Upvotes: 0

Josnidhin
Josnidhin

Reputation: 12504

According to whatsapp faq downloaded images show up in the gallery.

That means whatsapp is forcing the media scanner to run so they should be broadcasting ACTION_MEDIA_SCANNER_SCAN_FILE intent. So I guess creating a BroadcastReceiver for this intent might work.

Intent.getData has the path to the file.It might help you in differentiating between apps and file types.

The following Intents might also come in handy.

ACTION_MEDIA_SCANNER_FINISHED

ACTION_MEDIA_SCANNER_STARTED

Upvotes: 2

Related Questions