2014-09-24 65 views
1

我加入的圖像(JPEG和PNG)和音頻(MP3播放),動態文件到一個目錄,我列出了所有的文件圖像的水平滾動view.On點擊它會帶我們到另一個地方的活動點擊的圖像顯示在圖像view.My的問題是,我想啓動媒體播放器點擊.mp3文件。如何做到這一點?如何區分目錄中的mp3和jpeg文件?

如何知道選定的文件是圖像還是mp3文件?我想要點擊mp3文件啓動音頻播放器..怎麼做?

預先感謝您。

+0

將您的代碼粘貼到此處。你已經嘗試過了嗎? – XtreemDeveloper 2014-09-24 05:47:38

回答

0

我會look up the whole directory ..和搜索每一個文件名爲.jpg或MP3播放末與.endswith()

File sdCardRoot = Environment.getExternalStorageDirectory(); 
File yourDir = new File(sdCardRoot, "path"); 
for (File f : yourDir.listFiles()) { 
    if (f.isFile()) 
     String name = f.getName(); 
     if(name.endsWith(".mp3)){ 
      //Call MP3 player 
     } 
     else if(name.endsWith(".jpg") || name.endsWith(".png")){ 
      //Do something with your picture 
     } 
} 

,你應該能夠launch the mp3-player

Intent intent = new Intent(); 
intent.setAction(android.content.Intent.ACTION_VIEW); 
File file = new File(YOUR_SONG_URI); 
intent.setDataAndType(Uri.fromFile(file), "audio/*"); 
startActivity(intent); 
+0

這已經解決了我的問題,非常感謝:) – kiran 2014-09-24 06:49:34

0

嘗試像這樣

File sdCardRoot = Environment.getExternalStorageDirectory(); 
    File yourDir = new File(sdCardRoot, "path"); 
    File[] mp3Files = yourDir.listFiles(new FilenameFilter() { 
     public boolean accept(File dir, String name) 
     { 
      return (name.endsWith(".mp3")); 
     } 
    }); 
0
if(filename.contains(".mp3")){ 
// play that 
}else{ 
// Set the image 
} 
+0

如果我的圖片名稱像'image.mp3image.jpg'比??/ – MilapTank 2014-09-24 06:27:49

+0

謝謝你支持:) – kiran 2014-09-24 06:49:57

相關問題