android取得當前程式File與Cache路徑!

來源:互聯網
上載者:User

主介面就是顯示兩個button,分別進入File與Cache的目錄:

 

 

程式的架構為:

 

 

來看下主程式的代碼:

 

public class FileCacheActivity extends Activity implements OnClickListener<br />{<br /> private Button myButton1;<br /> private Button myButton2;<br /> private File cacheDir;<br /> private File fileDir;</p><p> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState)<br /> {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> myButton1 = (Button) findViewById(R.id.myButton1);<br /> myButton2 = (Button) findViewById(R.id.myButton2);</p><p> myButton1.setOnClickListener(this);<br /> myButton2.setOnClickListener(this);</p><p> /**<br /> * 取得目前Cache目錄<br /> */<br /> cacheDir = this.getCacheDir();</p><p> /**<br /> * 取得目前File目錄<br /> */<br /> fileDir = this.getFilesDir();<br /> }</p><p> /* 調用EX05_16_1並將路徑傳入 */<br /> private void showActivity(String path)<br /> {<br /> Intent intent = new Intent();<br /> intent.setClass(FileCacheActivity.this, FileCacheListActivity.class);</p><p> Bundle bundle = new Bundle();<br /> bundle.putString("path", path);<br /> intent.putExtras(bundle);</p><p> startActivity(intent);<br /> }</p><p> @Override<br /> public void onClick(View v)<br /> {<br /> // TODO Auto-generated method stub<br /> int viewId = v.getId();<br /> switch (viewId)<br /> {<br /> case R.id.myButton1:<br /> {<br /> String path = fileDir.getParent() + File.separator<br /> + fileDir.getName();<br /> showActivity(path);<br /> break;<br /> }<br /> case R.id.myButton2:<br /> {<br /> String path = cacheDir.getParent() + File.separator<br /> + cacheDir.getName();<br /> showActivity(path);<br /> break;<br /> }<br /> default:<br /> break;<br /> }<br /> }</p><p>}

 

然後是顯示listview的代碼:

 

public class FileCacheListActivity extends Activity implements<br /> OnItemClickListener<br />{<br /> private List<String> items = null;<br /> private String path;<br /> private ListView listview;</p><p> /** Called when the activity is first created. */<br /> public void onCreate(Bundle savedInstanceState)<br /> {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.mylist);</p><p> listview = (ListView) findViewById(R.id.list);<br /> listview.setOnItemClickListener(this);<br /> /**<br /> * 取得bundle<br /> */<br /> Bundle bunde = this.getIntent().getExtras();</p><p> /**<br /> * 取得上一個頁面傳入bundle裡的資料<br /> */<br /> path = bunde.getString("path");</p><p> this.setTitle(path);</p><p> /**<br /> * 去一個新的檔案目錄用來顯示目錄裡的所有檔案<br /> */<br /> File file = new File(path);<br /> fill(file.listFiles());// 列出該路徑下的所有檔案<br /> }</p><p> private void fill(File[] files)<br /> {<br /> items = new ArrayList<String>();<br /> if (files == null)<br /> {<br /> return;<br /> }</p><p> /* 取得檔案名稱放入ArrayList */<br /> for (File file : files)<br /> {<br /> items.add(file.getName());<br /> }</p><p> /* 將ArrayList放入ArrayAdapter */<br /> ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,<br /> android.R.layout.simple_list_item_1, items);<br /> listview.setAdapter(fileList);<br /> }</p><p> @Override<br /> public void onItemClick(AdapterView<?> l, View v, int position, long id)<br /> {<br /> File file = new File(path + File.separator + items.get(position));</p><p> if (file.isDirectory())<br /> {<br /> fill(file.listFiles());<br /> }<br /> }</p><p>}<br />

 

最後再來兩個xml檔案

main.xml

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /> <Button<br /> android:id="@+id/myButton1"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="@string/strList1"<br /> ><br /> </Button><br /> <Button<br /> android:id="@+id/myButton2"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="@string/strList2"<br /> ><br /> </Button><br /></LinearLayout>

 

mylist.xml

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout<br /> xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"><br /> <ListView<br /> android:id="@+id/list"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /> </ListView><br /></LinearLayout>

 

完了!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.