本章節(jié)翻譯自《Beginning-Android-4-Application-Development》,如有翻譯不當(dāng)?shù)牡胤剑凑堉赋觥?
原書購買地址 http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/
上一節(jié)介紹了如何把文件存儲到內(nèi)部設(shè)備。有的時(shí)候,需要把文件存儲到外部存儲設(shè)備,比如SD卡。因?yàn)镾D卡具有更大的存儲空間,同時(shí)也可以很容易的和其他用戶分享這些文件。
使用上一節(jié)的例子,把用戶輸入的文字保存在SD卡,修改onClick()事件。如下:
public class FilesActivity extends Activity { EditText textBox; static final int READ_BLOCK_SIZE = 100; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textBox = (EditText) findViewById(R.id.txtText1); InputStream is = this.getResources().openRawResource(R.raw.textfile); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = null; try { while ((str = br.readLine()) != null) { Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show(); } is.close(); br.close(); } catch (IOException e) { e.printStackTrace(); } } public void onClickSave(View view) { String str = textBox.getText().toString(); try { //---SD Card Storage--- File sdCard = Environment.getExternalStorageDirectory(); File directory = new File (sdCard.getAbsolutePath() + "/MyFiles"); directory.mkdirs(); File file = new File(directory, "textfile.txt"); FileOutputStream fOut = new FileOutputStream(file); /* FileOutputStream fOut = openFileOutput("textfile.txt", MODE_WORLD_READABLE); */ OutputStreamWriter osw = new OutputStreamWriter(fOut); //---write the string to the file--- osw.write(str); osw.flush(); osw.close(); //---display file saved message--- Toast.makeText(getBaseContext(), "File saved successfully!", Toast.LENGTH_SHORT).show(); //---clears the EditText--- textBox.setText(""); } catch (IOException ioe) { ioe.printStackTrace(); } } }上面的代碼中,使用getExternalStorageDirectory()方法去獲取SD卡的路徑。通常,在真機(jī)上面返回“/sdcard”,在模擬器上面返回"/mnt/sdcard"。但是,不要嘗試去寫死SD卡的路徑,因?yàn)槭謾C(jī)廠商有可能去給SD卡指定一個(gè)路徑。因此,確保使用getExternalStorageDirectory()方法去獲取SD卡的路徑。
然后,創(chuàng)建一個(gè)MyFiles的文件夾。最終,把文件保存在這個(gè)文件夾中。
從外部設(shè)備中加載文件,修改onClickLoad()方法:
public void onClickLoad(View view) { try { //---SD Storage--- File sdCard = Environment.getExternalStorageDirectory(); File directory = new File (sdCard.getAbsolutePath() + "/MyFiles"); File file = new File(directory, "textfile.txt"); FileInputStream fIn = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fIn); /* FileInputStream fIn = openFileInput("textfile.txt"); InputStreamReader isr = new InputStreamReader(fIn); */ char[] inputBuffer = new char[READ_BLOCK_SIZE]; String s = ""; int charRead; while ((charRead = isr.read(inputBuffer))>0) { //---convert the chars to a String--- String readString = String.copyValueOf(inputBuffer, 0, charRead); s += readString; inputBuffer = new char[READ_BLOCK_SIZE]; } //---set the EditText to the text that has been // read--- textBox.setText(s); Toast.makeText(getBaseContext(), "File loaded successfully!", Toast.LENGTH_SHORT).show(); } catch (IOException ioe) { ioe.printStackTrace(); } }
請注意,如果想要往SD卡中寫入文件,需要在AndroidManifest.xml中加入相關(guān)的權(quán)限:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.manoel.Files" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".FilesActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
執(zhí)行上述代碼,查看SD卡:
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
