2down vote | 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#filesExternalTo Filter files based on Name/Extension: How to acces sdcard and return and array with files off a specific format? |
| add a comment |
up vote1down vote | 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!!! |
| add a comment |
up vote0down vote | Updated function:: use this for new apiscall function like this:searchForFileInExternalStorage("filename.ext"); @implementationpublic 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;
} |
| add a comment |
up vote0down vote | 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);
} |
2down vote | 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#filesExternalTo Filter files based on Name/Extension: How to acces sdcard and return and array with files off a specific format? |
| add a comment |
up vote1down vote | 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!!! |
| add a comment |
up vote0down vote | Updated function:: use this for new apiscall function like this:searchForFileInExternalStorage("filename.ext"); @implementationpublic 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;
} |
| add a comment |
up vote0down vote | 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);
} |
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理