Android从SD卡中读取所有的文件

本文介绍了一种在Android应用中获取指定目录下所有文件的方法,并展示了如何使用这些文件填充ListView组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Add a Method GetFiles() to your program. Call it to get an ArrayList<> of all the files. You can then use it to populate your listview. You need to provide String argument DirectoryPath.

The Function:

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;
}

Usage Example:

@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
         }
    });
}

Make sure that the External Storage is Readable:http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

To Filter files based on Name/Extension: How to acces sdcard and return and array with files off a specific format?

share | improve this answer
 
 
What does android.R.layout.simple_list_item_1 refer to? –   SiKni8  Aug 4 '13 at 4:05
 
This is just an example, but if you are curious, I used a sub-layout simple_list_item_1 for each list item in my listview "filelist" –   Sheharyar  Aug 4 '13 at 16:06
 
Can you provide some suggestion for this question: stackoverflow.com/questions/18045035/… –   SiKni8  Aug 4 '13 at 16:28
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());
    }
}

may this would help!!!

share | improve this answer
 
 
thanks a lot. I'll try this one. :) –   Jethro  Apr 27 '11 at 8:02
 
but im trying to display it in a ListView? is it possible with your code? –   Jethro  Apr 27 '11 at 8:04
2 
ya, but u hav to create ur logic –   Jazz  Apr 27 '11 at 8:35

Updated function:: use this for new apis

call function like this:

 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;
        }
share | improve this answer
 

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);
    }

Add a Method GetFiles() to your program. Call it to get an ArrayList<> of all the files. You can then use it to populate your listview. You need to provide String argument DirectoryPath.

The Function:

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;
}

Usage Example:

@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
         }
    });
}

Make sure that the External Storage is Readable:http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

To Filter files based on Name/Extension: How to acces sdcard and return and array with files off a specific format?

share | improve this answer
 
   
What does android.R.layout.simple_list_item_1 refer to? –   SiKni8  Aug 4 '13 at 4:05
   
This is just an example, but if you are curious, I used a sub-layout simple_list_item_1 for each list item in my listview "filelist" –   Sheharyar  Aug 4 '13 at 16:06
   
Can you provide some suggestion for this question: stackoverflow.com/questions/18045035/… –   SiKni8  Aug 4 '13 at 16:28
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());
    }
}

may this would help!!!

share | improve this answer
 
   
thanks a lot. I'll try this one. :) –   Jethro  Apr 27 '11 at 8:02
   
but im trying to display it in a ListView? is it possible with your code? –   Jethro  Apr 27 '11 at 8:04
2 
ya, but u hav to create ur logic –   Jazz  Apr 27 '11 at 8:35

Updated function:: use this for new apis

call function like this:

 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;
        }
share | improve this answer
 

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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值