Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将文件从原始文件夹获取到listview?_Android_List_Mp3 - Fatal编程技术网

Android 如何将文件从原始文件夹获取到listview?

Android 如何将文件从原始文件夹获取到listview?,android,list,mp3,Android,List,Mp3,像这样的代码 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>

像这样的代码

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;
}
公共类歌曲管理器{
//SD卡路径
final String MEDIA_PATH=new String(“/sdcard/”)//这将切换到原始文件夹。。
private ArrayList songsList=new ArrayList();
//建造师
公共歌曲管理器(){
}
/**
*从SD卡读取所有mp3文件的功能
*并将详细信息存储在ArrayList中
* */
公共阵列列表getPlayList(){
文件主目录=新文件(媒体路径);
if(home.listFiles(新文件扩展筛选器()).length>0){
对于(文件:home.listFiles(新的FileExtensionFilter())){
HashMap宋=新HashMap();
song.put(“songTitle”,file.getName().substring(0,(file.getName().length()-4));
put(“songPath”,file.getPath());
//将每首歌曲添加到歌曲列表
歌曲列表。添加(歌曲);
}
}
//返回歌曲列表数组
返回歌曲列表;
}
但是从原始文件读取…

尝试:

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
        }
    }
}
更新:

看起来您正在尝试使用文件名及其对应路径构建哈希映射。这可能对sd卡中的通用文件有意义,但对原始或资产文件夹中的文件无效。这是因为它们包含在apk中,而apk基本上是一个zip文件。这意味着访问sd卡中的文件并不那么容易apk(尽管可以使用一些解压工具)


由于您没有明确说明您需要什么,因此很难知道。您可以使用上面的代码获取原始文件夹中的文件名并将其显示在列表中。此外,您还可以保存资源id。如果用户单击某个项目,您将获取该项目的资源id并使用InputStream加载文件,如上面的代码所示。您不能使用在您的示例中的文件类(因为正如我所说的,它们不是真正的文件),但是您可以使用Android资源类读取输入流中的原始资产。

在原始文件中只能读取一个文件,如何读取原始文件夹中的所有文件?@ DeWuuDeE再次更新。您可以考虑给出关于最终目标的更多信息。
/**
   * Method to read in a text file placed in the res/raw directory of the
   * application. The method reads in all lines of the file sequentially.
 */

public static void readRaw(Context ctx,int res_id) {

InputStream is = ctx.getResources().openRawResource(res_id);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr, 8192); // 2nd arg is buffer
                                                    // size

// More efficient (less readable) implementation of above is the
// composite expression
/*
 * BufferedReader br = new BufferedReader(new InputStreamReader(
 * this.getResources().openRawResource(R.raw.textfile)), 8192);
 */

try {
    String test;
    while (true) {
        test = br.readLine();
        // readLine() returns null if no more lines in the file
        if (test == null)
            break;
    }
    isr.close();
    is.close();
    br.close();
} catch (IOException e) {
    e.printStackTrace();
 }
 }
/**
   * Method to read in a text file placed in the res/raw directory of the
   * application. The method reads in all lines of the file sequentially.
 */

public static void readRaw(Context ctx,int res_id) {

InputStream is = ctx.getResources().openRawResource(res_id);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr, 8192); // 2nd arg is buffer
                                                    // size

// More efficient (less readable) implementation of above is the
// composite expression
/*
 * BufferedReader br = new BufferedReader(new InputStreamReader(
 * this.getResources().openRawResource(R.raw.textfile)), 8192);
 */

try {
    String test;
    while (true) {
        test = br.readLine();
        // readLine() returns null if no more lines in the file
        if (test == null)
            break;
    }
    isr.close();
    is.close();
    br.close();
} catch (IOException e) {
    e.printStackTrace();
 }
 }