0

In my application, when a user want to upload Audio, that user should only be able to choose .mp3 extension file. If the user choose any other file like .ppt, .pdf, it wont be allowed.

I have got this approved code for File explore from SD Card: Choose file dialog.
But I want only .mp3 extension files or list of .mp3 extension files.

So what should I do?

Community
  • 1
  • 1
CKnDROID
  • 123
  • 1
  • 3
  • 13
  • when I was working with many files(different filetypes as well) stored in my assets folder to select mp3 file with a particular name i used get the name dyanamically and attach ".mp3" at the end of the name. so this method worked for me. getAssets().openFd(audioName + ".mp3"); something similar may work for you with respect resources stored in the sdcard. – D'yer Mak'er Mar 26 '13 at 10:24
  • Did you try the accepted answer of the link you provided? It seems to fill your needs quite well – nicopico Mar 26 '13 at 10:28
  • Hii.. nicopico.. Yes I did.. but I want only .mp3 files.. either the user can choose only .mp3 file or get list of all .mp3 files from sd card when user want to attach and upload. – CKnDROID Mar 26 '13 at 10:44
  • Hii.. abhi_is_learning_andriod, My friend I strictly want to upload files from sd card. I am exploring my sd card and upload .mp3 file. – CKnDROID Mar 26 '13 at 10:46

2 Answers2

1

maybe you can check for files like this

File f = new File("/sdcard/"); 
for (File file : f.listFiles()) 
{
    if(file.getName().endsWith(".mp3"))
    {
        // do something with them, add them to list or whatever
    }
}
Marko Niciforovic
  • 3,561
  • 2
  • 21
  • 28
1

Check file pattern and then compare, if it is mp3 then add into arraylist, check below function, which will return list of mp3.

  Vector<String> mStrings =new Vector<String>();
 int mTotalimage=0;
public void fileExtension(File dir) {
       String mp3Pattern = ".mp3";


       File listFile[] = dir.listFiles();
            if (listFile != null) {
                for (int i = 0; i < listFile.length; i++) {
                    String mCheck=listFile[i].getAbsoluteFile().toString();
                    mCheck=mCheck.substring(mCheck.lastIndexOf("/")+1);
    System.out.println("listFile[i].getAbsoluteFile().toString()"+listFile[i].getAbsoluteFile().toString());
    System.out.println("mCheck..."+mCheck);
                    if (listFile[i].isDirectory() && !(mCheck.startsWith(".")) ) {
                        fileExtension(listFile[i]);
                    } else {

                      //fetch jpg from folders.
                      if (listFile[i].getName().endsWith(mp3Pattern)){
                          mStrings.add(listFile[i].getAbsolutePath().toString());
                          mTotalimage++;
                         //fetch png image from folders.
                        }
                    }
                }
            }
            System.out.println("size on return......."+mStrings.size());
        }
RobinHood
  • 10,897
  • 4
  • 48
  • 97
  • Thanx RobinHood, from your above code I've solved my problem. I hv just minor changed in one line: return (sel.isDirectory()||filename.endsWith(".mp3")); I understand your code, too.. Thanx for giving new idea for that.! :) – CKnDROID Mar 26 '13 at 11:13