6

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 return the filename selected to the main class. IF the returned file is an image file, it will be displayed in an ImageView and if the returned file is an audio file, it'll just display an icon.

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
Jethro
  • 61
  • 1
  • 1
  • 3
  • 9
    What have you done so far? Stack Overflow is not a _I-have-a-problem-provide-code_-site. You show us what you've done and we tell you whats wrong. – Octavian Helm Apr 27 '11 at 08:10
  • Please check this answer as a ref http://stackoverflow.com/questions/11285288/android-mediastore-get-distinct-folders-of-music-files/28473755#28473755 – Ashish Saini Feb 12 '15 at 10:13

6 Answers6

7

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)
        return null;
    else {
        for (int i=0; i<files.length; i++) 
            MyFiles.add(files[i].getName());
    }

    return MyFiles;
}

Usage Example:

@Override
public void onCreate() {
// Other Code

    ListView lv;
    ArrayList<String> FilesInFolder = GetFiles("/sdcard/somefolder");
    lv = (ListView)findViewById(R.id.filelist);

    lv.setAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, FilesInFolder));

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(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?

Community
  • 1
  • 1
Sheharyar
  • 73,588
  • 21
  • 168
  • 215
  • What does `android.R.layout.simple_list_item_1` refer to? – Si8 Aug 04 '13 at 04:05
  • 1
    This is just an example, but if you are curious, I used a sub-layout `simple_list_item_1 `for each list item in my `listview` "filelist" – Sheharyar Aug 04 '13 at 16:06
  • Can you provide some suggestion for this question: http://stackoverflow.com/questions/18045035/use-customadapter-to-change-how-listview-is-displayed – Si8 Aug 04 '13 at 16:28
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

  1. List all media files - How to list all media in Android?
  2. Start new Activity
  3. Android: Capturing the return of an activity
Community
  • 1
  • 1
Mojo Risin
  • 8,136
  • 5
  • 45
  • 58
  • I appreciate your response man. I have learned some basics already but im having a hard time dealing with the files (displayin) and displaying it on a ListView. – Jethro Apr 27 '11 at 08:08
  • Ok :). SO to solve this read 1. You have to use MediaStore to query all files it will return Cursor. You have to wrap the cursor in SimpleCursorAdapter and set this adapter to the ListView. – Mojo Risin Apr 27 '11 at 08:12
1
public class FileActivity extends ListActivity {
String str;
ArrayList<String> al;
ArrayAdapter<String> adapter;
ListView lv;

@SuppressLint("SdCardPath")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.file_view);
    Intent int1=getIntent();
    ArrayList<String> arr1=GetFiles(Environment.getExternalStorageDirectory().getPath());
    adapter= new ArrayAdapter<String>(getApplicationContext(),
                            android.R.layout.simple_expandable_list_item_1,arr1);
    lv = (ListView) findViewById(android.R.id.list);
    lv.setAdapter(adapter);
}  
private ArrayList<String> GetFiles(String path) {
    ArrayList<String> arr2=new ArrayList<String>();
    File file=new File(path);
    File[] allfiles=file.listFiles();
    if(allfiles.length==0) {
        return null;
    }
    else {
        for(int i=0;i<allfiles.length;i++) {
            arr2.add(allfiles[i].getName());
        }
    }
 return arr2; 
  }


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    }
Anagh Hegde
  • 327
  • 2
  • 14
Gulshad
  • 11
  • 1
1
File mfile=new File("/sdcard");
File[] list=mfile.listFiles();

System.out.println("list"+mfile.listFiles().length);
for(int i=0;i<mfile.listFiles().length;i++)
{
    if(list[i].isHidden())
    }
        System.out.println("hidden path files.."+list[i].getAbsolutePath());
    }
}

may this would help!!!

0

Updated function:: use this for new apis

call function like this:

 searchForFileInExternalStorage("filename.ext");  

@implementation

 public File searchForFileInExternalStorage(String filename) {
            File storage = Environment.getExternalStorageDirectory();

            return searchForFileInFolder(filename, storage);
        }

        public File searchForFileInFolder(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 name
                    if (child.getName().equals(filename)) {
                        return child;
                    }
                }
            }

            return null;
        }
DPP
  • 12,716
  • 3
  • 49
  • 46
0

this is a correct solution for you! or i give you a link for this stuff!

public class SongsManager {
// SDCard Path
//choose your path for me i choose sdcard
final String MEDIA_PATH = new String("/sdcard/");
private ArrayList<hashmap<string, string="">> songsList = new ArrayList<hashmap<string, string="">>();

// Constructor
public SongsManager(){

}

/**
 * Function to read all mp3 files from sdcard
 * and store the details in ArrayList
 * */
public ArrayList<hashmap<string, string="">> getPlayList(){
    File home = new File(MEDIA_PATH);

    if (home.listFiles(new FileExtensionFilter()).length > 0) {
        for (File file : home.listFiles(new FileExtensionFilter())) {
            HashMap<string, string=""> song = new HashMap<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 array
    return songsList;
}

/**
 * Class to filter files which are having .mp3 extension
 * */
//you can choose the filter for me i put .mp3
class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3") || name.endsWith(".MP3"));
    }
}
 }
ddd
  • 11
  • 1