Android ApiDemos樣本解析(50):Content->Resources->E

來源:互聯網
上載者:User

應用程式也可以使用外部儲存空間,比如SD卡來存取資源。本例介紹了如何使用外部儲存空間來建立,刪除檔案。

本例使用SD卡的三個位置來建立檔案,兩個是應用程式私人的,隨著應用程式的刪除被刪除,另外一個是共用的SD卡上Picuture目錄。兩個私人目錄說是私人,但由於是在SD卡,別的應用程式也是可以訪問的,只是它建立的目錄名和應用程式相關,一般來說可以保證目錄名的唯一性。

 

類Environment 可以用來查詢一些環境變數,比如是否含有外部儲存空間(SD卡),外部儲存空間的目錄名等。下面代碼取得外部儲存空間上Picture目錄名:

[java] 
//createExternalStoragePublicPicture  
File path = Environment.getExternalStoragePublicDirectory( 
 Environment.DIRECTORY_PICTURES); 
File file = new File(path, "DemoPicture.jpg"); 
//createExternalStoragePublicPicture
File path = Environment.getExternalStoragePublicDirectory(
 Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
而Context類的getExternalFilesDir 可以取得應用程式在外部儲存空間的私人目錄,getExternalFilesDir可以帶參數,也可以使用Null。

[java] 
//createExternalStoragePrivatePicture  
File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
File file = new File(path, "DemoPicture.jpg"); 
  
 ... 
//createExternalStoragePrivateFile  
File file = new File(getExternalFilesDir(null), "DemoFile.jpg"); 
//createExternalStoragePrivatePicture
File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
 
 ...
//createExternalStoragePrivateFile
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
三種方法建立的對應著SD卡的位置如所示:


此外本例還同時說明了一種自訂View以及動態在ViewGroup在添加View的方法,本例使用的兩個Layout資源:

R.layout.external_storage為整體布局,根View為一Id為R.id.layout 的LinearLayout。

R.layout.external_storage_item 為一自訂控制項,有多個View組成:

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent” android:layout_height=”wrap_content”>
<TextView android:id=”@+id/label” style=”?android:attr/textAppearanceMediumInverse”
android:layout_width=”match_parent” android:layout_height=”wrap_content”
android:layout_weight=”0″
android:padding=”4dip”
android:singleLine=”true”
android:color=”?android:attr/textColorPrimaryInverse”
android:background=”#888″ />
< TextView android:id=”@+id/path” style=”?android:attr/textAppearanceSmall”
android:layout_width=”match_parent” android:layout_height=”wrap_content”
android:layout_weight=”0″
android:padding=”4dip”
android:singleLine=”true” />
<LinearLayout
android:orientation=”horizontal”
android:layout_width=”match_parent” android:layout_height=”wrap_content”>
<View
android:layout_width=”0dip”
android:layout_height=”0dip”
android:layout_weight=”1″ />
< Button android:id=”@+id/create”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:text=”@string/create” />
<View
android:layout_width=”0dip”
android:layout_height=”0dip”
android:layout_weight=”1″ />
< Button android:id=”@+id/delete”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:text=”@string/delete” />
<View
android:layout_width=”0dip”
android:layout_height=”0dip”
android:layout_weight=”1″ />
< /LinearLayout>
< /LinearLayout>

上面三個粗體定義的無ID的View為兩個按鈕Create, Delete之間的預留位置。

 

方法createStorageControls 使用R.layout.external_storage_item 為模板重新定義文字框顯示以及Create,Delete的回呼函數。

然後動態將三個自訂群組件添加到Layout中,這就構成了最後的UI。


[java] 
mLayout.addView(mExternalStoragePublicPicture.mRoot); 
mLayout.addView(mExternalStoragePrivatePicture.mRoot); 
mLayout.addView(mExternalStoragePrivateFile.mRoot); 
mLayout.addView(mExternalStoragePublicPicture.mRoot);
mLayout.addView(mExternalStoragePrivatePicture.mRoot);
mLayout.addView(mExternalStoragePrivateFile.mRoot);

 作者:mapdigit
 

 

 

聯繫我們

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