簡單實現瀏覽Android SD卡中的檔案

來源:互聯網
上載者:User

標籤:android   style   http   io   ar   os   sp   for   java   

----Main.java

public class Main extends Activity {private TextView textView;private Button button;private ListView listView;public File currentParentFile;public File[] currentFiles;public static  String sdcardDir ;static {try {//sd卡的路徑sdcardDir = Environment.getExternalStorageDirectory().getCanonicalPath();} catch (IOException e) {e.printStackTrace();}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);textView = (TextView) findViewById(R.id.textView1);button = (Button) findViewById(R.id.button1);listView = (ListView) findViewById(R.id.listView1);File root = new File(sdcardDir);if(root.exists()){currentParentFile = root;currentFiles = root.listFiles();updateListView(currentFiles);}listView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {if (currentFiles[position].isFile())return;File[] tmp = currentFiles[position].listFiles();if (tmp == null || tmp.length == 0) {Toast.makeText(Main.this, "當前路徑無效,或沒有檔案",Toast.LENGTH_SHORT).show();} else {currentParentFile = currentFiles[position];currentFiles = tmp;updateListView(currentFiles);}}});button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {if (!currentParentFile.getCanonicalPath().equals(sdcardDir)) {currentParentFile = currentParentFile.getParentFile();currentFiles = currentParentFile.listFiles();updateListView(currentFiles);}elsereturn;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});}private void updateListView(File[] files) {List<Map<String, Object>> itemps = new ArrayList<Map<String, Object>>();for (int i = 0; i < files.length; i++) {Map<String, Object> listItem = new HashMap<String, Object>();if (files[i].isDirectory())listItem.put("icon", R.drawable.folder);elselistItem.put("icon", R.drawable.file);listItem.put("name", files[i].getName());itemps.add(listItem);}SimpleAdapter simpleAdapter = new SimpleAdapter(this, itemps,R.layout.listitem, new String[] { "icon", "name" }, new int[] {R.id.imageView1, R.id.text });listView.setAdapter(simpleAdapter);try {textView.setText("當前路徑為:"+currentParentFile.getCanonicalPath());} catch (IOException e) {e.printStackTrace();}}}

布局檔案----main.xml

<RelativeLayout 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"    tools:context="${relativePackage}.${activityClass}" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="當前路徑:" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:text="返回上一級目錄" />    <ListView        android:id="@+id/listView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_above="@+id/button1"        android:layout_alignParentLeft="true"        android:layout_below="@+id/textView1" >    </ListView></RelativeLayout>

效果:

listview的每一條的布局:

---listitem.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:src="@drawable/ic_launcher" />    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_marginTop="15dp"        android:layout_toRightOf="@+id/imageView1"        android:text="TextView" /></RelativeLayout>

demo運行效果:


簡單實現瀏覽Android SD卡中的檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.