Android-simple sdcard file browsing, android-sdcard

Source: Internet
Author: User
Tags addall

Android-simple sdcard file browsing, android-sdcard

Function: allows you to browse the folders and files on your mobile phone. The code is usually too simple.

 

First view Layout

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "com. example. fanlei. myservicedemo. filePathActivity "> <android. support. v7.widget. toolbar android: layout_width = "match_parent" android: layout_height = "? Attr/actionBarSize "android: background =" #000 "> <Button android: id =" @ + id/button "android: layout_width =" wrap_content "android: layout_height = "match_parent" android: text = "back to upper-level"/> </android. support. v7.widget. toolbar> <ListView android: id = "@ + id/listView" android: layout_width = "match_parent" android: layout_height = "match_parent"> </ListView> </LinearLayout>

Main Function

1 package com. example. fanlei. myservicedemo; 2 3 import android. annotation. targetApi; 4 import android. OS. build; 5 import android. OS. bundle; 6 import android. OS. environment; 7 import android. support. v7.app. actionBarActivity; 8 import android. view. view; 9 import android. widget. adapterView; 10 import android. widget. arrayAdapter; 11 import android. widget. button; 12 import android. widget. listView; 13 im Port android. widget. toast; 14 import java. io. file; 15 import java. util. arrayList; 16 import java. util. list; 17 18 public class FilePathActivity extends ActionBarActivity {19 20 21 private ListView; 22 private File currentParent; // record parent folder 23 private File [] currentFiles; // record the array of all files in the current path 24 private List <String> fileName; // file or folder name 25 26 private ArrayAdapter arrayAdapter; 27 private Butt On button; 28 29 @ TargetApi (Build. VERSION_CODES.KITKAT) 30 @ Override 31 protected void onCreate (Bundle savedInstanceState) {32 super. onCreate (savedInstanceState); 33 setContentView (R. layout. activity_file_path); 34 35 // determine whether there is a wood with sdcard card 36 if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {37 initView (); 38 initData (); 39 initEvent (); 40} else {41 Toast. makeText (this, "No sd Card ", Toast. LENGTH_LONG ). show (); 42} 43 44} 45 46 private void initView () {47 listView = (ListView) findViewById (R. id. listView); 48 button = (Button) findViewById (R. id. button); 49} 50 51 private void initData () {52 currentParent = Environment. getExternalStorageDirectory (); 53 currentFiles = currentParent. listFiles (); 54 fileName = new ArrayList <> (); 55 List <String> temp = filesToFileName (curre NtFiles); 56 fileName. addAll (temp); 57 arrayAdapter = new ArrayAdapter (this, android. r. layout. simple_list_item_1, fileName); 58 listView. setAdapter (arrayAdapter); 59} 60 61 private void initEvent () {62 listView. setOnItemClickListener (new AdapterView. onItemClickListener () {63 @ Override 64 public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {65 if (! CurrentFiles [position]. isFile () {66 File [] temp = currentFiles [position]. listFiles (); 67 if (temp = null | temp. length = 0) {68 Toast. makeText (FilePathActivity. this, "No file under this file", Toast. LENGTH_LONG ). show (); 69} else {70 currentParent = currentFiles [position]; 71 currentFiles = temp; 72 fileName. clear (); 73 fileName. addAll (filesToFileName (currentFiles); 74 arrayAdapter. notifyDataSetChanged (); 75} 76} 77} 78}); 79 // return key 80 button. setOnClickListener (new View. OnClickListener () {81 @ Override 82 public void onClick (View v) {83 if (! CurrentParent. getPath (). equals (Environment. getExternalStorageDirectory (). getPath () {84 currentParent = currentParent. getParentFile (); 85 currentFiles = currentParent. listFiles (); 86 fileName. clear (); 87 fileName. addAll (filesToFileName (currentFiles); 88 arrayAdapter. notifyDataSetChanged (); 89} 90} 91}); 92} 93 // returns an array 94 private List of File names <String> filesToFileName (File [] files) {95 List <St Ring> fileName = new ArrayList <> (); 96 for (int I = 0; I <files. length; I ++) {97 if (! Files [I]. getName (). substring (0, 1 ). equals (". ") {// here I put". "does not display 98 fileName. add (files [I]. getName (); 99} 100} 101 return fileName; 102} 103}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.