user474026
user474026

Reputation: 99

path to files in raw or drawable

Hello I am using a tutorial for a media player here

I want to change the media path location to point to all the files in the raw folder so I can keep my songs within the apk. I tried several times to do this with no success. Can someone help. Here code i need to adjust.

package com.androidhive.musicplayer;


import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;

// Need to redirect the path below to the raw or drawable folder
public class SongsManager {
    // SDCard Path
    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
     * */
    class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".mp3") || name.endsWith(".MP3"));
        }
    }
} 

Any help will be appreciated. Thanks

Upvotes: 1

Views: 1303

Answers (2)

Ridcully
Ridcully

Reputation: 23655

AlexN is right, 1 MB is (still) the limit for file sizes in assets, res etc.

Upvotes: 0

AlexN
AlexN

Reputation: 2554

Actually, Android's Raw folder works a little different than filesystem. For instance you shouldn't work with it in a File paradigm. Instead you can use Context's Resources class, for instance openRawResource http://developer.android.com/reference/android/content/res/Resources.html#openRawResource(int)

However, working with such API to store large files, like audio - can be performance-costly and unpredictable. For example I had troubles to open a file from /assets/ folder, if it's size > 1MB. And it seems to be platform limitation, at least for those time.

So generally I suggest to download files from the network, if it's necessary. Or, if the size of files will be definetelly small - precopy them from Resources to Internal/External storage.

Hope it helps, Good luck

Upvotes: 2

Related Questions