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?
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With