android之寫檔案到sd卡

來源:互聯網
上載者:User

1、main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <EditText        android:id="@+id/numET"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint="請輸入檔案名稱" />    <EditText        android:id="@+id/contentET"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint="請輸入檔案內容"//提示文本        android:inputType="textMultiLine"        android:minLines="3" />//最小為3行,多了自動變大    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="horizontal" >                <Button android:id="@+id/sdcBT"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="儲存到sd卡"            android:onClick="onClick"            />                <Button android:id="@+id/romBT"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="儲存到手機"            android:onClick="onClick"            />    </LinearLayout></LinearLayout>

2、MainActivity

package com.njupt.file1;import android.os.Bundle;import android.os.Environment;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private EditText numET;private EditText contentET;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);numET = (EditText) findViewById(R.id.numET);    contentET = (EditText) findViewById(R.id.contentET);}@Overrideprotected void onResume() {super.onResume();//Environment.getExternalStorageState().擷取sd卡的狀態,判斷是安裝,還是移除findViewById(R.id.sdcBT).setEnabled(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}public void onClick(View v){try{String name = numET.getText().toString();String content = contentET.getText().toString();FileService service = new FileService();switch(v.getId()){case R.id.sdcBT:service.saveToSDCard(name,content);break;case R.id.romBT:service.saveToRom(name,content);break;}Toast.makeText(getApplicationContext(), "儲存成功", Toast.LENGTH_SHORT).show();}catch(Exception e){e.printStackTrace();Toast.makeText(getApplication(), "dacard出異常", Toast.LENGTH_SHORT).show();}}}

3、FileService

package com.njupt.file1;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import android.os.Environment;public class FileService {public void saveToSDCard(String name, String content) { FileOutputStream fos = null; try{//Environment.getExternalStorageDirectory()。擷取sd卡的路徑File file = new File(Environment.getExternalStorageDirectory(),name);    fos = new FileOutputStream(file);        fos.write(content.getBytes());}catch(Exception e){e.printStackTrace();}finally{try {fos.close();} catch (IOException e) {e.printStackTrace();}}}public void saveToRom(String name, String content) {// TODO Auto-generated method stub}}

4、AndroidManifest.xml

最後在AndroidManifest.xml配上

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

聯繫我們

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