1

I am adding images(jpeg and png) and audio(.mp3) files dynamically into a directory,i am listing out all the files in the horizontal scroll view.On click of images it will take us to another activity where the clicked images is displaying in the image view.My question is, i want to start media player on click of .mp3 file.How to do that?

how to know selected file is image or mp3 file? and i want to start audio player on click of mp3 file..How to do that?

Thank you in advance.

kiran
  • 31
  • 7

3 Answers3

1

I would look up the whole directory.. and search every filename for .jpg or .mp3 at the end with .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
        }
}

and you should be able to launch the mp3-player with

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);
Community
  • 1
  • 1
JDurstberger
  • 4,127
  • 8
  • 31
  • 68
0

Try something like this

    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"));
        }
    });
Panther
  • 8,938
  • 3
  • 23
  • 34
0
if(filename.contains(".mp3")){
//  play that
}else{
// Set the image
}
Ram
  • 114
  • 12