2011-04-27 39 views
7

我想創建一個按鈕,點擊後會轉到一個類,使用ListView顯示SD卡中的所有媒體文件。如何在ListView中的SD卡上顯示文件?

從列表中選擇後,它將返回選擇的主文件名。如果返回的文件是圖像文件,它將顯示在ImageView中,並且如果返回的文件是音頻文件,則它只會顯示一個圖標。

+9

到目前爲止你做了什麼?堆棧溢出不是一個我有問題的提供代碼站點。你告訴我們你做了什麼,我們告訴你什麼是錯的。 – 2011-04-27 08:10:13

+0

請檢查這個答案作爲參考 http://stackoverflow.com/questions/11285288/android-mediastore-get-distinct-folders-of-music-files/28473755#28473755 – 2015-02-12 10:13:38

回答

1
File mfile=new File("/sdcard"); 
File[] list=mfile.listFiles(); 

System.out.println("list"+mfile.listFiles().length); 
for(int i=0;i<mfile.listFiles().length;i++) 
{ 
    if(list[i].isHidden()) 
    } 
     System.out.println("hidden path files.."+list[i].getAbsolutePath()); 
    } 
} 

這可能會有所幫助!

+0

非常感謝。我會嘗試這一個。 :) – Jethro 2011-04-27 08:02:24

+0

但即時通訊嘗試顯示它在一個ListView?你的代碼有可能嗎? – Jethro 2011-04-27 08:04:49

+2

雅,但你必須創建你的邏輯 – 2011-04-27 08:35:41

2

首先我強烈建議你先閱讀一些有關android的教程,以便了解基礎知識。你必須實現以下要做到這一點

  1. 列表中的所有媒體文件 - How to list all media in Android?
  2. Start new Activity
  3. Android: Capturing the return of an activity
+0

我感謝你的迴應男人。我已經學會了一些基礎知識,但我很難處理文件(displayin)並將其顯示在ListView上。 – Jethro 2011-04-27 08:08:33

+0

好的:)。所以要解決這個問題1.你必須使用MediaStore來查詢它將返回Cursor的所有文件。您必須將光標封裝在SimpleCursorAdapter中,並將此適配器設置爲ListView。 – 2011-04-27 08:12:04

8

添加方法GetFiles()到您的程序。調用它來獲得所有文件的ArrayList<>。然後,您可以使用它來填充您的listview。您需要提供字符串參數DirectoryPath

的功能:

public ArrayList<String> GetFiles(String DirectoryPath) { 
    ArrayList<String> MyFiles = new ArrayList<String>(); 
    File f = new File(DirectoryPath); 

    f.mkdirs(); 
    File[] files = f.listFiles(); 
    if (files.length == 0) 
     return null; 
    else { 
     for (int i=0; i<files.length; i++) 
      MyFiles.add(files[i].getName()); 
    } 

    return MyFiles; 
} 

用法示例:

@Override 
public void onCreate() { 
// Other Code 

    ListView lv; 
    ArrayList<String> FilesInFolder = GetFiles("/sdcard/somefolder"); 
    lv = (ListView)findViewById(R.id.filelist); 

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

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      // Clicking on items 
     } 
    }); 
} 

確保外部存儲是否可讀:http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

要根據名稱/延伸過濾器文件:How to acces sdcard and return and array with files off a specific format?

+0

'android.R.layout.simple_list_item_1'指的是什麼? – Si8 2013-08-04 04:05:22

+1

這僅僅是一個例子,但如果你很好奇,我在'listview'' filelist「 – Sheharyar 2013-08-04 16:06:09

+0

中爲每個列表項目使用了一個子佈局'simple_list_item_1'你能提供一些關於這個問題的建議:http:// stackoverflow。 COM /問題/ 18045035 /使用-customadapter到變化如何,列表視圖,則顯示的 – Si8 2013-08-04 16:28:29

0

更新功能::用這個新的API

通話功能是這樣的:

searchForFileInExternalStorage("filename.ext"); 

@implementation

public File searchForFileInExternalStorage(String filename) { 
      File storage = Environment.getExternalStorageDirectory(); 

      return searchForFileInFolder(filename, storage); 
     } 

     public File searchForFileInFolder(String filename, File folder) { 
      File[] children = folder.listFiles(); 
      File result; 

      for (File child : children) { 
       if (child.isDirectory()) { 
        result = searchForFileInFolder(filename, child); 
        if (result != null) { 
         return result; 
        } 
       } else { 
        // replace equals by equalsIgnoreCase if you want to ignore the 
        // case of the file name 
        if (child.getName().equals(filename)) { 
         return child; 
        } 
       } 
      } 

      return null; 
     } 
1
public class FileActivity extends ListActivity { 
String str; 
ArrayList<String> al; 
ArrayAdapter<String> adapter; 
ListView lv; 

@SuppressLint("SdCardPath") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.file_view); 
    Intent int1=getIntent(); 
    ArrayList<String> arr1=GetFiles(Environment.getExternalStorageDirectory().getPath()); 
    adapter= new ArrayAdapter<String>(getApplicationContext(), 
          android.R.layout.simple_expandable_list_item_1,arr1); 
    lv = (ListView) findViewById(android.R.id.list); 
    lv.setAdapter(adapter); 
} 
private ArrayList<String> GetFiles(String path) { 
    ArrayList<String> arr2=new ArrayList<String>(); 
    File file=new File(path); 
    File[] allfiles=file.listFiles(); 
    if(allfiles.length==0) { 
     return null; 
    } 
    else { 
     for(int i=0;i<allfiles.length;i++) { 
      arr2.add(allfiles[i].getName()); 
     } 
    } 
return arr2; 
    } 


@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 
    } 
0

這是您的正確解決方案!或者我給你一個link這個東西!

public class SongsManager { 
// SDCard Path 
//choose your path for me i choose sdcard 
final String MEDIA_PATH = new String("/sdcard/"); 
private ArrayList<hashmap<string, string="">> songsList = new ArrayList<hashmap<string, string="">>(); 

// Constructor 
public SongsManager(){ 

} 

/** 
* Function to read all mp3 files from sdcard 
* and store the details in ArrayList 
* */ 
public ArrayList<hashmap<string, string="">> getPlayList(){ 
    File home = new File(MEDIA_PATH); 

    if (home.listFiles(new FileExtensionFilter()).length > 0) { 
     for (File file : home.listFiles(new FileExtensionFilter())) { 
      HashMap<string, string=""> song = new HashMap<string, string="">(); 
      song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4))); 
      song.put("songPath", file.getPath()); 

      // Adding each song to SongList 
      songsList.add(song); 
     } 
    } 
    // return songs list array 
    return songsList; 
} 

/** 
* Class to filter files which are having .mp3 extension 
* */ 
//you can choose the filter for me i put .mp3 
class FileExtensionFilter implements FilenameFilter { 
    public boolean accept(File dir, String name) { 
     return (name.endsWith(".mp3") || name.endsWith(".MP3")); 
    } 
} 
} 
相關問題