In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this "Android how to achieve File Resource Manager" article, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Android how to achieve File Resource Manager" article.
First do the requirements analysis (the function realized):
1.ListView begins to display subdirectories and files under the sdcard directory.
two。 Click on the file, and the Toast displays "Click on the file"
3. Click on the directory, enter the subdirectory, and display the subdirectories and files under the subdirectory.
The 4.back key rolls back to the upper directory.
5. Exception handling:
5.1 if the sdcard is not inserted, the list is not displayed and the user is prompted that the sdcard should be inserted
5.2 access to the upper directory of sdcard is not allowed
The following begins to be implemented:
There are two layouts:
1. Main layout: file_list.xml
The layout is simple: a ListView control is placed, and it should be noted here that no other child controls can be placed under the ListView tag. The content is displayed through sublayouts and Adapter.
Sub-layout file_list_item.xml in 2.ListView
The sublayout is also simple by displaying an icon on the left in the horizontal direction to display the folder or file icon and the file name on the right.
3.Activity code (function points are written in comments)
ICON and Filename keys in public class FileExplorerActivity extends Activity {/ / Adapter pair constant private static final String ICON = "icon"; private static final String FILENAME = "filename"; data private File root;// folder root node private File [] currentFiles displayed by ListView SimpleAdapter adapter;// adapter private List data;// in the list of ListView SimpleAdapter adapter;// files showing the current path on private TextView currentTv;//ListView / / all files (including folders) under the root node private File currentPath;// records the current node @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_file_explorer); currentTv = (TextView) findViewById (R.id.currentTv); fileLv = (ListView) findViewById (R.id.fileLv) / / get the root node root-- > / mnt/sdcard root = getFileSystemRoot (); / / get the information of the first screen if (root! = null) {/ / get the file list data = getFileListFromSdcard (root) from / mnt/sdcard;} else {/ / if no sdcard is mounted, prompt the user data = new ArrayList () HashMap map = new HashMap (); map.put (ICON, R.drawable.none); map.put (FILENAME, "tease me, insert cards"); data.add (map) } / / create Adapater adapter = new SimpleAdapter (this, data, R.layout.file_list_item, new String [] {ICON, FILENAME}, new int [] {R.id.icon, R.id.filename}); fileLv.setAdapter (adapter) / / bind event fileLv.setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView parent, View view, int position, long id) {/ / Click the listview item, if it is a directory, then proceed to the next level. If it is a file, do not deal with File currentPosFile = currentFiles [position] If (currentPosFile.isDirectory ()) {getFileListFromSdcard (currentPosFile);} else {Toast.makeText (FileExplorerActivity.this, "you clicked on the folder", Toast.LENGTH_LONG) .show ();}) } / * intercept back key returns * @ return * / @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {if (KeyEvent.KEYCODE_BACK = = keyCode) {File parentFile = currentPath.getParentFile () / / cannot exceed the top try {if (parentFile.getCanonicalPath (). ToString (). Equals ("/ mnt")) {Toast.makeText (this, "stop pressing, you're home", Toast.LENGTH_LONG) .show (); return true;} else {getFileListFromSdcard (parentFile) } catch (IOException e) {e.printStackTrace ();}} return super.onKeyDown (keyCode, event) } private File getFileSystemRoot () {/ / first get whether the SD card is loaded with if (Environment.MEDIA_MOUNTED.equals (Environment.getExternalStorageState () {/ / get the SD card path root-> / mnt/sdcard root = Environment.getExternalStorageDirectory ();} else {Toast.makeText (this, "tease me, insert the card", Toast.LENGTH_LONG). Show () } return root;} / * get the list of files in Sdcard * @ return * / private List getFileListFromSdcard (File root) {try {currentPath = root; currentTv.setText (root.getCanonicalPath (). ToString ());} catch (IOException e) {e.printStackTrace () } List list = new ArrayList (); currentFiles = root.listFiles (); / / list all files and directories in the current directory for (File f: currentFiles) {HashMap map = new HashMap (); String fileName = f.getName (); int icon; if (f.isDirectory ()) {icon = R.drawable.dir Map.put (ICON, icon);} else if (f.isFile ()) {icon = R.drawable.file; map.put (ICON, icon);} map.put (FILENAME, fileName); list.add (map) } / / empty the original data list, then put the list in, and notify adapter if (data! = null) {data.clear (); data.addAll (list); adapter.notifyDataSetChanged ();} return list;}}
Running effect:
Function Outlook:
The above code is achieved by streamlining features, and it is quite simple to add the following features:
1. Delete function of folders and files
two。 Renaming of folders and files
3. The classification of files calls the App view function
4. File details display function
The above is about the content of this article on "how to achieve document Resource Manager in Android". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.