Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get files from raw folder to listview?

Tags:

android

list

mp3

like this code..

public class SongsManager {
// SDCard Path
final String MEDIA_PATH = new String("/sdcard/");//this will I switch to raw folder..

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;
}

but read from raw files...

like image 326
Dewa Yudie Avatar asked Dec 27 '22 08:12

Dewa Yudie


1 Answers

Try that:

public void getRawFiles(){
    Field[] fields = R.raw.class.getFields();
    // loop for every file in raw folder
    for(int count=0; count < fields.length; count++){

        int rid = fields[count].getInt(fields[count]);

        // Use that if you just need the file name
        String filename = fields[count].getName();

        // Use this to load the file
        try {
            Resources res = getResources();
            InputStream in = res.openRawResource(rid);

            byte[] b = new byte[in.available()];
            in.read(b);
            // do whatever you need with the in stream
        } catch (Exception e) {
            // log error
        }
    }
}

BTW, maybe not totally related to your question, but if you want to read from the SD card you shouldn't write a hard-coded path. It won't work for many devices. You should use that instead:

String sdcard = Environment.getExternalStorageDirectory();

Update:

Looks like you are trying to build a hash map with filenames and their corresponding paths. This may make sense for generic files in the sd card, but it doesn't work for files in the raw or assets folders. This is because they are contained in your apk, which is basically a zip file. This means it's not that easy to access the files within the apk (though it is possible by using some unzip tool).

Since you didn't say what do you need exactly, is difficult to know. You can use the code above to get the filenames in the raw folder and show them in a list. Additionally, you can save the resource id. If the user clicks an item, you get the resource id for that item and load the file with the InputStream, as in the code above. You can't use the File class as in your example (because as I said, they are not really files), but you can read the raw assets with an InputStream using the android Resources class.

like image 87
Esparver Avatar answered Dec 28 '22 20:12

Esparver