,我有以下方法的文件):獲取lastModifiedDate()和文件大小(單位ArrayList中
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final RelativeLayout mFrame3 = (RelativeLayout) inflater.inflate(R.layout.ptrip, container, false);
File folder = new File(Environment.getExternalStorageDirectory() + "/tc/");
ListView lv;
ArrayList<String> FilesInFolder = GetFiles(folder.getAbsolutePath());
Toast.makeText(getActivity(), FilesInFolder.get(0).toString(), 2000).show(); //display filename (0)
////////////////////////
///////////////////////
//would this work:
//for (int i = 0; i <= FilesInFolder.length(); i++) {
//long lastTime = FilesInFolder[0].lastModified();
//String dateString = DateFormat.format("MM/dd/yyyy", new Date(lastTime)).toString(); //last modified date?
//String sizeString = getReadableSize(FolderInFiles[i].length()); //file size?
//}
//////////////////////
//////////////////////
lv = (ListView) mFrame3.findViewById(R.id.lvFiles);
lv.setAdapter(new ArrayAdapter<String>(getActivity(), 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
//Toast.makeText(getActivity(), "Item " + (position + 1) + ": ID" + v, Toast.LENGTH_SHORT).show();
}
});
return mFrame3;
}
我的GetFolder功能是:
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++) {
if (files[i].getName().endsWith(".tol")) {
long lastTime = files[i].lastModified();
String dateString = DateFormat.format("MM/dd/yyyy", new Date(lastTime)).toString();
String sizeString = getReadableSize(files[i].length());
//String fileName = files[i].getName().substring(0, files[i].getName().lastIndexOf("."));
MyFiles.add(files[i].getName());
//MyFiles.add("Trip Name: " + fileName + "\nTrip Taken On: " + dateString + "\n" + sizeString);
}
}
}
return MyFiles;
}
如何獲得內部的fileSize
和lastModifiedDate
onCreateMethod
?我註釋掉GetFolder
方法中的fileSize
和lastModifiedDate
,因爲我試圖只獲取文件名,所以我可以讀取onCreate
方法中的文件,以在MultiLine EditText中顯示它的ASCII內容。
//display filename (0)
////////////////////////
///////////////////////
//would this work:
//for (int i = 0; i <= FilesInFolder.length(); i++) {
//long lastTime = FilesInFolder[0].lastModified();
//String dateString = DateFormat.format("MM/dd/yyyy", new Date(lastTime)).toString(); //last modified date?
//String sizeString = getReadableSize(FolderInFiles[i].length()); //file size?
//}
//////////////////////
//////////////////////
不要使用'String'來表示一個文件,使用'List'並且只有當你需要顯示文件名才能獲得每個文件的名字。 –
zapl
現在該函數正在以擴展名返回文件名。 – Si8