Karthick S
Karthick S

Reputation: 3304

Accessing Whatsapp Media folder in Android app

I am writing an app that will access the WhatsApp/Media/WhatsApp Images directory located in a user's android phone and sets different photos as background depending on the user's location.

I see that the location directory WhatsApp is directly under Device Storage when I access it using a Folder Manager kind of an app installed on my phone. However, I am not able to find it when I do a search under / from my app's code. My code is here:

private void findWhatsappMediaDirectories() {
    String whatsappMediaDirectoryName = "/";
    displayTreeStructure(whatsappMediaDirectoryName);
}

private void displayTreeStructure(String whatsappMediaDirectoryName) {
    File whatsappMediaDirectory = new File(whatsappMediaDirectoryName);
    Log.e(this.getClass().toString(), "In " + whatsappMediaDirectoryName);
    File[] mediaDirectories = whatsappMediaDirectory.listFiles();

    if (mediaDirectories == null) {
        Log.e(this.getClass().toString(), whatsappMediaDirectoryName + " does not have any files");
    }
    else if (mediaDirectories.length != 0) {
        for (File mediaDirectory : mediaDirectories) {
            if (mediaDirectory.getName().equals("WhatsApp")) {
                if (mediaDirectory.isDirectory()) {
                    Log.e(this.getClass().toString(), mediaDirectory.getAbsolutePath());
                    displayTreeStructure(mediaDirectory.getAbsolutePath());
                }
            }
        }
    }
}

This keeps failing with no directory name displayed. Can someone please point out what the path should be to access the location?

Upvotes: 4

Views: 6659

Answers (2)

Shaktisinh Jadeja
Shaktisinh Jadeja

Reputation: 1457

Environment.getExternalStorageDirectory().getAbsolutePath() + "/WhatsApp/Media/.Statuses"

Upvotes: 0

Karthick S
Karthick S

Reputation: 3304

Found the solution. The problem was in my understanding of the concept of internal and external storage in Android devices. The right way to access WhatsApp/Media directory is as follows:

File whatsappMediaDirectoryName = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/WhatsApp/Media");

Upvotes: 4

Related Questions