How To Display Files On The Sd Card In A Listview?
I would like to create a button that when clicked will go to a class that displays all media files from an SD card using a ListView. After selecting from the list it will then retu
Solution 1:
Add a Method GetFiles()
to your program. Call it to get an ArrayList<>
of all the files. You can then use it to populate your listview
. You need to provide String argument DirectoryPath
.
The Function:
public ArrayList<String> GetFiles(String DirectoryPath) {
ArrayList<String> MyFiles = new ArrayList<String>();
File f = new File(DirectoryPath);
f.mkdirs();
File[] files = f.listFiles();
if (files.length == 0)
returnnull;
else {
for (int i=0; i<files.length; i++)
MyFiles.add(files[i].getName());
}
return MyFiles;
}
Usage Example:
@OverridepublicvoidonCreate() {
// Other CodeListView lv;
ArrayList<String> FilesInFolder = GetFiles("/sdcard/somefolder");
lv = (ListView)findViewById(R.id.filelist);
lv.setAdapter(newArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, FilesInFolder));
lv.setOnItemClickListener(newAdapterView.OnItemClickListener() {
publicvoidonItemClick(AdapterView<?> parent, View v, int position, long id) {
// Clicking on items
}
});
}
Make sure that the External Storage is Readable:http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
To Filter files based on Name/Extension:How to acces sdcard and return and array with files off a specific format?
Solution 2:
First i strongly suggest you read some tutorials about android first so you get the basics. You have to implement the following to do this
Solution 3:
publicclassFileActivityextendsListActivity {
String str;
ArrayList<String> al;
ArrayAdapter<String> adapter;
ListView lv;
@SuppressLint("SdCardPath")
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file_view);
Intent int1=getIntent();
ArrayList<String> arr1=GetFiles(Environment.getExternalStorageDirectory().getPath());
adapter= newArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_expandable_list_item_1,arr1);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
}
privateArrayList<String> GetFiles(String path) {
ArrayList<String> arr2=newArrayList<String>();
File file=newFile(path);
File[] allfiles=file.listFiles();
if(allfiles.length==0) {
returnnull;
}
else {
for(int i=0;i<allfiles.length;i++) {
arr2.add(allfiles[i].getName());
}
}
return arr2;
}
@OverrideprotectedvoidonListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stubsuper.onListItemClick(l, v, position, id);
}
Solution 4:
Updated function:: use this for new apis
call function like this:
searchForFileInExternalStorage("filename.ext");
@implementation
publicFilesearchForFileInExternalStorage(String filename) {
File storage = Environment.getExternalStorageDirectory();
returnsearchForFileInFolder(filename, storage);
}
publicFilesearchForFileInFolder(String filename, File folder) {
File[] children = folder.listFiles();
File result;
for (File child : children) {
if (child.isDirectory()) {
result = searchForFileInFolder(filename, child);
if (result != null) {
return result;
}
} else {
// replace equals by equalsIgnoreCase if you want to ignore the// case of the file nameif (child.getName().equals(filename)) {
return child;
}
}
}
returnnull;
}
Solution 5:
this is a correct solution for you! or i give you a link for this stuff!
publicclassSongsManager {
// SDCard Path//choose your path for me i choose sdcard
final StringMEDIA_PATH = newString("/sdcard/");
privateArrayList<hashmap<string, string="">> songsList = newArrayList<hashmap<string, string="">>();
// ConstructorpublicSongsManager(){
}
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
* */publicArrayList<hashmap<string, string="">> getPlayList(){
File home = newFile(MEDIA_PATH);
if (home.listFiles(newFileExtensionFilter()).length > 0) {
for (File file : home.listFiles(newFileExtensionFilter())) {
HashMap<string, string=""> song = newHashMap<string, string="">();
song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
song.put("songPath", file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list arrayreturn songsList;
}
/**
* Class to filter files which are having .mp3 extension
* *///you can choose the filter for me i put .mp3classFileExtensionFilterimplementsFilenameFilter {
publicbooleanaccept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}
Post a Comment for "How To Display Files On The Sd Card In A Listview?"