如何从原始文件夹中的文件的ListView? [英] How to get files from raw folder to listview?

查看:93
本文介绍了如何从原始文件夹中的文件的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样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...

推荐答案

试一下:

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卡读取你不应该写一硬codeD路径。它不会为许多设备工作。您应该使用来代替:

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();

更新:

看起来你正在试图建立一个哈希映射文件名和其相应的路径。这可就在SD卡一般文件感,但它不会在原料或资产文件夹文件。这是因为它们包含在您的APK,这基本上是一个zip文件。这意味着它不是那么容易的APK中访问文件(尽管它可能是通过使用一些解压缩工具)。

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).

既然你没有说什么你到底需要,是很难知道。你可以用code以上,以获得文件名在原文件夹中,并将其显示在列表中。此外,还可以节省资源ID。如果用户点击一个项目,你会得到该项目的资源ID与InputStream中加载文件,如code以上。你不能使用File类作为您的例子(因为正如我所说,他们是不是真的文件),但你可以使用Android的资源类的InputStream读取原始资产。

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.

这篇关于如何从原始文件夹中的文件的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆