2012-07-01 83 views
5

尋找從Android MediaStore獲取音樂文件的文件夾路徑以獲取輸入目錄路徑的方法。所以實際上問題是關於如何對MediaStore進行適當的查詢。Android MediaStore獲取音樂文件的不同文件夾

這裏是一個例子。 我當前的代碼:

String dirPath="/mnt/sdcard/Music/"; 
    String selection =MediaStore.Audio.Media.DATA +" like ?"; 
    String[] projection = {MediaStore.Audio.Media.DATA};  
    String[] selectionArgs={dirPath+"%"}; 
    Cursor cursor = this.managedQuery(
     MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
     projection, 
     selection, 
     selectionArgs, 
     null); 
    List<String> songs = new ArrayList<String>(); 
    while(cursor.moveToNext()) 
     songs.add(cursor.getString(0)); 

因此,我們必須在songs列表水木清華這樣的:

/mnt/sdcard/Music/song1.mp3 
/mnt/sdcard/Music/song2.mp3 
/mnt/sdcard/Music/FolderWithSongs/song3.mp3 
/mnt/sdcard/Music/AnotherFolderWithSongs/song4.mp3 
/mnt/sdcard/Music/AndOneMoreFolder/song5.mp3 
/mnt/sdcard/Music/AndOneMoreFolder/song6.mp3 
/mnt/sdcard/Music/AndOneMoreFolder/song7.mp3 
/mnt/sdcard/Music/AndOneMoreFolder/SomeFolder/song8.mp3 
/mnt/sdcard/Music/AndOneMoreFolder/SomeFolder/song9.mp3 

什麼,我實際上是尋找 - 獲得一個級別的,例如DISTINCT列表嵌套路徑從dirPath這樣的:

/mnt/sdcard/Music/FolderWithSongs/ 
/mnt/sdcard/Music/AnotherFolderWithSongs/ 
/mnt/sdcard/Music/AndOneMoreFolder/ 

如果此處將當前文件夾中的文件 - /mnt/sdcard/Music/song1.mp3/mnt/sdcard/Music/song2.mp3 - 這也是可以接受的。

任何想法?

編輯 我發現了兩個可能的解決方案: Android list music by folders and play themConstruct a tree structure from list of string paths ,但我認爲更好的辦法來解決這個問題是可能的。

回答

0

這可能工作: - 使用walkDir方法來搜索您的存儲位置的所有音樂文件,那麼你可以從的getPath獲取每個文件的路徑()方法 - 所有路徑的文件使用subsString()方法來獲得級別1文件夾名稱 - 將所有文件夾名稱存儲在ArrayList中使用Hashset刪除重複文件夾名稱(帶條件)

2

這是我的解決方案。

  1. 掃描所有音頻文件EX)Cursor mCursor = getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, ...
  2. 宣佈新的數組
  3. 文件夾名稱= MediaStore.Audio.Media.DATA - MediaStore.Audio.Media.DISPLAY_NAME
  4. 將新成數組變量
  5. 陣列是一種獨特的文件夾列表
+0

我調整了代碼一點,所以它更容易閱讀;你能檢查並確保我沒有刪除任何增加意義的東西嗎? – andrewsi

+0

@andrewsi,很好。謝謝。 – jeonggu

+0

github上的任何示例代碼? – TheOnlyAnil

9

你將不得不使用這兩種技術。 MediaStore和文件資源管理器。

public class MainActivity extends ActionBarActivity { 
private File file; 
private List<String> myList; 
private ListView listView; 
private TextView pathTextView; 
private String mediapath = new String(Environment.getExternalStorageDirectory().getAbsolutePath()); 

private final static String[] acceptedExtensions= {"mp3", "mp2", "wav", "flac", "ogg", "au" , "snd", "mid", "midi", "kar" 
    , "mga", "aif", "aiff", "aifc", "m3u", "oga", "spx"}; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    listView=(ListView) findViewById(R.id.pathlist); 
    pathTextView=(TextView) findViewById(R.id.path); 

    myList = new ArrayList<String>(); 

    String root_sd = Environment.getExternalStorageDirectory().toString(); 
    Log.e("Root",root_sd); 

    String state = Environment.getExternalStorageState(); 
    File list[] = null ; 
    /* if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // we can read the External Storage... 
     list=getAllFilesOfDir(Environment.getExternalStorageDirectory()); 
    }*/ 

    pathTextView.setText(root_sd); 

    file = new File(root_sd) ;  
    list = file.listFiles(new AudioFilter()); 
    Log.e("Size of list ","" +list.length); 
    //LoadDirectory(root_sd); 

    for(int i=0; i< list.length; i++) 
    { 

     String name=list[i].getName(); 
    int count =  getAudioFileCount(list[i].getAbsolutePath()); 
    Log.e("Count : "+count, list[i].getAbsolutePath()); 
    if(count!=0) 
    myList.add(name); 
     /*int count=getAllFilesOfDir(list[i]); 
     Log.e("Songs count ",""+count); 

     */ 

    } 


    listView.setAdapter(new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, myList)); 

    listView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
       long arg3) { 
      File temp_file = new File(file, myList.get(position)); 

      if(!temp_file.isFile())   
      { 
       //LoadDirectory(myList.get(position)); 

       file = new File(file, myList.get(position)); 
       File list[] = file.listFiles(new AudioFilter()); 

       myList.clear(); 

       for(int i=0; i< list.length; i++) 
       { 
        String name=list[i].getName(); 

        int count =  getAudioFileCount(list[i].getAbsolutePath()); 
         Log.e("Count : "+count, list[i].getAbsolutePath()); 
         if(count!=0) 
         myList.add(name); 
        /*int count=getAllFilesOfDir(list[i]); 
        Log.e("Songs count ",""+count); 
        if(count!=0) 
         myList.add(name);*/ 
       } 

       pathTextView.setText(file.toString()); 
       //Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_LONG).show(); 
       listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, myList)); 

      } 


     } 
    }); 



} 

private int getAudioFileCount(String dirPath) { 

     String selection =MediaStore.Audio.Media.DATA +" like ?"; 
     String[] projection = {MediaStore.Audio.Media.DATA};  
     String[] selectionArgs={dirPath+"%"}; 
     Cursor cursor = this.managedQuery(
      MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      projection, 
      selection, 
      selectionArgs, 
      null); 
     return cursor.getCount(); 
    } 


@Override 
public void onBackPressed() { 
    try { 
     String parent = file.getParent().toString(); 
     file = new File(parent) ;   
     File list[] = file.listFiles(new AudioFilter()); 

     myList.clear(); 

     for(int i=0; i< list.length; i++) 
     { 
      String name=list[i].getName(); 
      int count =  getAudioFileCount(list[i].getAbsolutePath()); 
       Log.e("Count : "+count, list[i].getAbsolutePath()); 
       if(count!=0) 
       myList.add(name); 
      /*int count=getAllFilesOfDir(list[i]); 
      Log.e("Songs count ",""+count); 
      if(count!=0)*/ 

     } 
     pathTextView.setText(parent); 
    // Toast.makeText(getApplicationContext(), parent,   Toast.LENGTH_LONG).show(); 
     listView.setAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, myList)); 
    } catch (Exception e) { 
     finish(); 
    } 


} 



// class to limit the choices shown when browsing to SD card to media files 
public class AudioFilter implements FileFilter { 

    // only want to see the following audio file types 
    private String[] extension = {".aac", ".mp3", ".wav", ".ogg", ".midi", ".3gp", ".mp4", ".m4a", ".amr", ".flac"}; 

    @Override 
    public boolean accept(File pathname) { 

     // if we are looking at a directory/file that's not hidden we want to see it so return TRUE 
     if ((pathname.isDirectory() || pathname.isFile()) && !pathname.isHidden()){ 
      return true; 
     } 

     // loops through and determines the extension of all files in the directory 
     // returns TRUE to only show the audio files defined in the String[] extension array 
     for (String ext : extension) { 
      if (pathname.getName().toLowerCase().endsWith(ext)) { 
       return true; 
      } 
     } 

     return false; 
    }  
} 



} 

.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:textSize="21sp" 
    android:typeface="monospace" 
    android:id="@+id/path" 
    android:layout_width="fill_parent" 
    android:layout_height="100dp" 
    android:layout_margin="10dp" 
    android:gravity="bottom" 
    android:text="PATH : " /> 

<View 
    android:layout_width="fill_parent" 
    android:layout_height="2dp" 
    android:background="#111111" /> 

<ListView android:id="@+id/pathlist" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
</ListView> 

</LinearLayout> 

權限在manifiest文件:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+0

悲傷的演示。在線返回logcat錯誤:list = file。listFiles(new AudioFilter()); – TheOnlyAnil

+0

在我的設備上完美運行 –

+1

正是我在找的東西。這個答案應該有更多upvotes。做得很好。 – crysis

相關問題