2014-09-24 67 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

相关问题