2017-05-14 122 views
1

我有項目,我不知道如何獲取文件夾中的所有文件名。我在Andoid Studio工作。我只需要使函數getAllFilesInFolder(string folderPath)。如果您編寫所需的所有進口,我也會很高興。代碼我使用:如何獲取Java文件夾中的所有文件

package com.my.appPackage; 

import android.app.Activity; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

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

     String[] files = getAllFilesInFolder("/sdcard/folderIUse"); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.LargeTextInList, files); 
     ListView list = (ListView) findViewById(R.id.listView); 
     list.setAdapter(adapter); 

     list.setOnItemClickListener(new ItemList()); 
    } 
    class ItemList implements AdapterView.OnItemClickListener{ 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 
      ViewGroup vg = (ViewGroup) view; 
      TextView tv = (TextView) vg.findViewById(R.id.LargeTextInList); 
      Toast.makeText(MainActivity.this, tv.getText().toString(), Toast.LENGTH_SHORT).show(); 
     } 
    } 

} 

回答

1

這可能會實現:

ArrayList<String> result = new ArrayList<String>(); //ArrayList cause you don't know how many files there is 
    File folder = new File("PATH/TO/YOUR/FOLDER/AS/STRING"); //This is just to cast to a File type since you pass it as a String 
    File[] filesInFolder = folder.listFiles(); // This returns all the folders and files in your path 
    for (File file : filesInFolder) { //For each of the entries do: 
     if (!file.isDirectory()) { //check that it's not a dir 
      result.add(new String(file.getName())); //push the filename as a string 
     } 
    } 

    return result; 
0
File directory = new File(path); 
File[] files = directory.listFiles(); 
for (int i = 0; i < files.length; i++) 
{ 
    //do something 
} 

path應該是這樣的: /存儲/模擬/ 0 /安卓/數據

0

從邁赫蘭Zamani答案將工作,但記得檢查文件類型。 請記住,不要僅僅檢查擴展名「abc.txt」。 abc文件可能是PDF文件,但有人通過重命名來更改其擴展名。 爲安全起見,您必須檢查其MIME類型。例如,在Java 7中,您現在可以使用Files.probeContentType(path)。這只是一個例子,您可能需要在線搜索更好的解決方案。只是試圖給你想法

你可能需要檢查它是否是另一個目錄。 例如file.isFile()/ file.isFolder()。

2

比方說,你要聚集在storage/emulated/0/DCIM和「DCIM」 futher有兩個目錄,假設Screenshotspictures。這是你會怎麼做的所有文件。

public void getAllFiles(File parent) { 
    List<File> directories = new ArrayList<>(); 
    List<File> files = new ArrayList<>(); 
    //adding initial parent whose files we want to fetch 
    directories.add(parent); 
    while (directories.size() != 0) { 
     File f = directories.remove(0); 
     if (f.isDirectory()) { 
      if (f.list().length > 0) { 
       //directory filter to filter out directories if any 
       List<File> directoryList = Arrays.asList(f.listFiles(directoryFilter)); 
       //file filter to filter out files if any 
       List<File> fileList = Arrays.asList(f.listFiles(fileFilter)); 
       //adding directories to directory list 
       directories.addAll(directoryList); 
       //adding files to file list 
       files.addAll(fileList); 
      } 
     } 
    } 
} 

FilenameFilter fileFilter = new FilenameFilter() { 
    @Override 
    public boolean accept(File file, String s) { 
    return file.isFile(); 
    } 
    }; 

    FilenameFilter directoryFilter = new FilenameFilter() { 
     @Override 
     public boolean accept(File file, String s) { 
      return file.isDirectory(); 
     } 
    }; 

這樣一個有剛剛通過的文件夾名稱的功能得到files object.Hope所有文件這有助於

相關問題