I have been trying to make a music player application,and as I have just started android, Could any one tell me how can I accces the music files in my sdcard, I have given the location of the sdcard as "\sdcard\" in my String variable MEDIA_PATH ,but what after that? How should the system access the music files put it more simply the files ending with .mp3 format,I don't know which function to use. I'd appreciate your suggestions, thanks.
You can get all the mp3 files from rooted sdcard by the following code.
File home = Environment.getExternalStorageDirectory();
if (home.listFiles( new Mp3Filter()).length > 0) {
for (File file : home.listFiles( new Mp3Filter())) {
songs.add(file.getAbsolutePath());
}
ArrayAdapter<String> songList = new ArrayAdapter<String>
(this,R.layout.song_item,songs);
setListAdapter(songList);
}
class Mp3Filter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return (name.endsWith(".mp3"));
}
}
Here songs is a ArrayList which stores the actual path of the song.
Use FileFilter for scanning the .mp3 files from sdcard. like:
//Check media mounted or not
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
File sdCardDir=new File("/sdcard");
File sdCardDir=Environment.getExternalStorageDirectory();
ArrayList<File>files=getFiles(file);
private ArrayList<File> getFiles(Filefile){
File files[]=sdCardDir.listFiles(filter);
for(File f:files){
if(f.isDirectory()&&f.canRead()){
getFiles(f);
}else if(f.isFile()){
videlList.add(f);
}
}
//USE FileFilter
FileFilter filter=new FileFilter(){
#Override
public boolean accept(File f){
returnf.isDirectory()||f.getName().matches("^.*?//.(mp3)$");
}
}
Related
Here I am trying to get pdf and csv files from WhatsApp Documents folder but not getting any instead of printing only Sent and Private folders only but I want all files from WhatsApp Documents
Please help
File srcFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Android/media/"+"com.whatsapp" + "/WhatsApp/Media/WhatsApp Documents/");
File[] allFiles = srcFile.listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String filename) {
System.out.println("filename="+filename);
return (filename.toLowerCase().endsWith(".pdf") && filename.toLowerCase().endsWith(".csv"));
}
});
list all files from WhatsApp Documents folder
I am working on app that is required to read the recover to deleted data. I search a lot but could not find a single solution.
Then I got to know that there is a director "com.sec.android.gallery3d" where all images are saved in small thumbnails.
My question can I read images?
I checked this sample code. But its only reading those directories which is public like DCIM, notification,Down etc
File file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
public ArrayList<File> listf(File xfile, ArrayList<File> files) {
// get all the files from a directory
File[] fList = xfile.listFiles();
for (File file : fList) {
if (file.isFile()) {
files.add(file);
} else if (file.isDirectory()) {
listf(file, files);
}
}
return files;
}```
I have created an ArrayList to scan .mp3 files from external storage:
final ArrayList<File> myplaylist0 = findSongs0(Environment.getExternalStorageDirectory());
Function findsongs0 is shown below:
public ArrayList<File> findSongs0(File root){
ArrayList<File> al=new ArrayList<File>();
File[] files = root.listFiles();
for(File singleFile : files){
if(singleFile.isDirectory() && !singleFile.isHidden()){
al.addAll(findSongs(singleFile));
}
else
{
if(singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")){
al.add(singleFile);
}
}
}
return al;
}
The above codes give me list of all .mp3 files from my phone storage.
Now, my question is, how to scan mp3 files from the particular folder instead of whole storage. What should I modify in the above code??
Your argument should be like
new File(Environment.getExternalStorageDirectory()+"/"+"<your_mp3_folder>")
i.e
File myfile=new File(Environment.getExternalStorageDirectory()+"/"+"<your_mp3_folder>")
final ArrayList<File> myplaylist0 = findSongs0(myfile);
I am trying to program my own file manager for android. What I want to know is that how can I read the file names of all the files present in external/internal storage?
I want to read the file names and show them in a listview so the user could see what files are present in what folder. I know this thing is going to work in recursive manner as I have to read the content of sub directories as well.
This is what you have to do. Before going to write please refer the File class in java. This will help you to clear lot of things.
Below is the snippet that provides the list of files.
File directory = new File(path);
File[] listFiles = directory.listFiles();
if (listFiles != null) {
for (File file : listFiles) {
if (file.isDirectory())
// do the stuff what you need
else if (file.isFile()) {
// do the stuff what you need
}
}
}
}
Following code will give you list of files in android sdcard:
/**
* Return list of files from path. <FileName, FilePath>
*
* #param path - The path to directory with images
* #return Files name and path all files in a directory, that have ext = "jpeg", "jpg","png", "bmp", "gif"
*/
private List<String> getListOfFiles(String path) {
File files = new File(path);
FileFilter filter = new FileFilter() {
private final List<String> exts = Arrays.asList("jpeg", "jpg",
"png", "bmp", "gif");
#Override
public boolean accept(File pathname) {
String ext;
String path = pathname.getPath();
ext = path.substring(path.lastIndexOf(".") + 1);
return exts.contains(ext);
}
};
final File [] filesFound = files.listFiles(filter);
List<String> list = new ArrayList<String>();
if (filesFound != null && filesFound.length > 0) {
for (File file : filesFound) {
list.add(file.getName());
}
}
return list;
}
You can call same method to get sub directory file also.
I need to acces the sdcard and return some files of different formats. The location will be input by the user. How can I do this programmatically?
Simondid,
I believe this is what you are looking for.
Accessing the SDCard:
reading a specific file from sdcard in android
Keep in mind checking the media availability:
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
Creating a file filter:
http://www.devdaily.com/blog/post/java/how-implement-java-filefilter-list-files-directory
Example of a mp3 file filter, Create the following filter class:
import java.io.*;
/**
* A class that implements the Java FileFilter interface.
* It will filter and grab only mp3
*/
public class Mp3FileFilter implements FileFilter
{
private final String[] okFileExtensions =
new String[] {"mp3"};
public boolean accept(File file)
{
for (String extension : okFileExtensions)
{
if (file.getName().toLowerCase().endsWith(extension))
{
return true;
}
}
return false;
}
}
Then based on the earlier post of accessing the sdcard you would use the filter like this:
File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard, "path/to/the/directory/with/mp3");
//THIS IS YOUR LIST OF MP3's
File[] mp3List = dir.listFiles(new Mp3FileFilter());
NOTE: The code is rough you probably want to make sure the sdcard is available as mentioned above