2013-03-06 71 views
4

這樣的代碼..如何從原始文件夾獲取文件到列表視圖?

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

但是從原始文件讀取...

回答

7

試一下:

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,也許不是完全與你的問題,但如果你想從SD卡上讀取,你不應該寫一個硬編碼的路徑。它不適用於很多設備。你應該是改用:

String sdcard = Environment.getExternalStorageDirectory(); 

更新:

看起來你正在試圖建立一個哈希映射文件名及其相應的路徑。這對於SD卡中的通用文件可能有意義,但對於原始文件夾或資產文件夾中的文件不起作用。這是因爲它們包含在你的apk中,它基本上是一個zip文件。這意味着訪問apk文件並不容易(儘管可以使用一些解壓縮工具)。

既然你沒有說完全需要什麼,很難知道。您可以使用上面的代碼獲取原始文件夾中的文件名並將它們顯示在列表中。另外,您可以保存資源ID。如果用戶單擊一個項目,您將獲得該項目的資源ID並使用InputStream加載文件,如上面的代碼中所示。你不能像你的例子那樣使用File類(因爲正如我所說的,它們不是真正的文件),但是你可以使用android Resources類使用InputStream讀取原始資源。

+0

那隻能讀取原始文件中的一個文件,如何讀取原始文件夾中的所有文件? – 2013-03-06 14:41:25

+0

@DewaYudie我編輯了我的答案,看看更新。 – Esparver 2013-03-06 14:42:58

+0

我不明白在我的代碼中實現,你能幫我嗎? – 2013-03-06 16:04:21

3
/** 
    * 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(); 
} 
} 
相關問題