2012-12-04 66 views
1

我有SD卡上的MP3文件。如何在選擇文件時從SD卡獲取文件的路徑?從SD卡的文件路徑

動態!...就像用戶點擊列表視圖中的文件獲取其變量以供使用。

public class PlayListActivity extends ListActivity { 
    // Songs list 
    public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.playlist);  
     ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();  
     SongsManager plm = new SongsManager(); 
     // get all songs from sdcard 
     this.songsList = plm.getPlayList(); 

     // looping through playlist 
     for (int i = 0; i < songsList.size(); i++) { 
      // creating new HashMap 
      HashMap<String, String> song = songsList.get(i); 

      // adding HashList to ArrayList 
      songsListData.add(song); 
     }  
     // Adding menuItems to ListView 
     ListAdapter adapter = new SimpleAdapter(this, songsListData, 
       R.layout.playlist_item, new String[] { "songTitle" }, new int[] { 
         R.id.songTitle }); 

     setListAdapter(adapter); 

     // selecting single ListView item 
     ListView lv = getListView(); 
     // listening to single listitem click 
     lv.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // getting listitem index 
       int songIndex = position; 

       // Starting new intent 
       Intent in = new Intent(getApplicationContext(), 
         AndroidBuildingMusicPlayerActivity.class); 
       // Sending songIndex to PlayerActivity 
       in.putExtra("songIndex", songIndex); 
       setResult(100, in); 
       // Closing PlayListView 
       finish(); 
      } 
     });  
    } 
} 
+0

http://whathaveyoutried.com? – njzk2

+0

我嘗試了硬編碼值!... –

+0

現在需要從用戶 –

回答

6

我想你想從一個打開文件對話框的文件,請查看以下鏈接

參考:Choose File Dialog

你可以得到SD卡的路徑下面命令的幫助:

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath(); 
String fileName = "myFile.mp3"; 

那麼路徑將是

String path = baseDir + "/your folder(s)/" + fileName; 

參考是:Android how to use Environment.getExternalStorageDirectory()

或者你可以嘗試:

new File("/mnt/external_sd/your folder(s)../file.mp3");//get a file from SD card 

參考:How can I get external SD card path for Android 4.0+?

0

要得到你應該使用類似的文件這

public static byte[] readFile (String file, Context c) throws IOException { 
    File f = new 
    File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), file); 
    if (f.exists()) 
     return readFile(f); 
    else return new byte[0]; 
} 

然後你可以用你得到的字節解碼文件。

0

我認爲這個代碼將與您一起...嘗試它,並按照我的結果

公共類SongsManager {

final String MEDIA_PATH = new String("/sdcard/"); 
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); 

// Constructor 
public SongsManager(){ 

} 

/**d 
* Function to read all mp3 files from sdcard 
* and store the details in ArrayList 
* */ 
public ArrayList<HashMap<String, String>> getPlayList(){ 
     File List[] = Environment.getExternalStorageDirectory().listFiles(); 

    File home; 

    int index=0; 

     while(index<List.length) 
     { 
      home = new File(List[index].getAbsolutePath().toString()); 
      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); 
      } 
      index++; 
     } 
    } 
    // 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")); 
    } 
} 

}

0

我已經拿到後解決這個4天。請注意以下幾點,同時給予路徑,文件類的Android(Java):

  1. 的使用路徑內(下稱Android的外部)存儲

    String path="/storage/sdcard0/myfile.txt"; 
    
  2. 存儲卡

    path="/storage/sdcard1/myfile.txt"; 
    
  3. 在Manifest文件中提及權限。

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    
  4. 首先檢查文件長度以確認。

  5. 檢查ES文件資源管理器中有關sdcard0的路徑& sdcard1是相同的,或者......例如:

    File file=new File(path); 
    long=file.length(); //in Bytes 
    
相關問題