How can we get the list of directories from a URL. e.g. from abc.com/myfolders? in Android.
If there are following folders in a URL
- abc.com/myfolders/folder1
- abc.com/myfolders/folder2
- abc.com/myfolders/folder3
- abc.com/myfolders/folder4
then how can the list be populated with
folder1
folder2
folder3
folder4
Try with the following code
String absolutePath = "abc.com/myfolders/folder1";
String directoryName = null;
if((absolutePath != null) && (absolutePath.length()>0)){
int indexOfFileSeparator = absolutePath.lastIndexOf("/");
if (indexOfFileSeparator >= 0) {
directoryName = absolutePath.substring(indexOfFileSeparator + 1);
}
}
System.out.println("...shortStr..." + directoryName);
You can't get a list of folders from a URL, unless the URL is configured to respond with the extra path elements that are valid. URLs are not necessarily a reflection of a server's file system, and therefore folder structure.
It sounds like you want something similar to the standard way some web servers allow for file browsing. I.e., when you navigate to
http://server/a/path/
the web server responds with a HTML page that lists the contents of that folder on the server.
Without that, you cannot find out the 'folders' within a URL.
If I've missed the point, then any of the other answers describing String concatenation should work.
String shortStr = longStr.substring(longStr.lastIndexOf("/") + 1);
This should do it.
I know it has been too late but i am posting this answer for other user's who is looking for same.
You can write PHP web service to get a list of file's located in particular folder of server.
<?php
$path = realpath('uploadAudio');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
if (strpos($filename,'extension of file like (.mp3) or (.txt)') !== false)
$filenm= basename($filename);
?>
<a href = "yourserverlink/Foldername/foldername/<?php echo $filenm ?>"><?php echo $filenm ?></a><br/>
<?php
}
?>
and then call this service in your java or android code.