Android SDCard操作(文件读写,容量计算) :http://zhuyonghui116.blog.hexun.com/56778119_d.html
Android中读写文件: http://blog.csdn.net/cocodehouse/article/details/5974288
增加权限:<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>
String sDStateString = android.os.Environment.getExternalStorageState();
// 拥有可读可写权限
if (sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {
… …
}
然后用FileInputStream和FileOutputStream进行读写。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Xml代码 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.file.rw" 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:name=".ReadWriteFilesActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
Xml代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="a.txt" /> <Button android:id="@+id/button1" android:layout_width="70dp" android:layout_height="wrap_content" android:text="Load" /> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" > <requestFocus /> </EditText> <Button android:id="@+id/button2" android:layout_width="68dp" android:layout_height="wrap_content" android:text="Save" /> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
Java代码 package com.file.rw; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class ReadWriteFilesActivity extends Activity { /** Called when the activity is first created. */ private Button button1,button2; private EditText editText1,editText2; private TextView textview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1=(Button)findViewById(R.id.button1); button2=(Button)findViewById(R.id.button2); editText1 = (EditText)findViewById(R.id.editText1); editText2 = (EditText)findViewById(R.id.editText2); textview =(TextView)findViewById(R.id.textview); //load file from sd card button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub textview.setText("Load............"); String sdStatus = android.os.Environment.getExternalStorageState(); if(sdStatus.equalsIgnoreCase(android.os.Environment.MEDIA_MOUNTED)){ textview.setText(editText1.getText()); File rootDir = android.os.Environment.getExternalStorageDirectory(); File readFile = new File(rootDir.getAbsolutePath()+File.separator+editText1.getText()); if(!readFile.exists()){ textview.setText(editText1.getText()+"不存在."); }else{ try { FileInputStream in = new FileInputStream(readFile); byte[] buffer = new byte[in.available()]; in.read(buffer); in.close(); editText2.setText(new String(buffer)); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }//end of android.os.Environment.MEDIA_MOUNTED } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(editText1.getText()==null||editText1.getText().toString().trim().length()==0){ textview.setText("第一个输入框没有输入文件名."); return; } if(editText2.getText()==null||editText2.getText().toString().trim().length()==0){ textview.setText("第而个输入框没有输入文件内容."); return; } String sdStatus = android.os.Environment.getExternalStorageState(); if(android.os.Environment.MEDIA_MOUNTED.equalsIgnoreCase(sdStatus)){ File root = android.os.Environment.getExternalStorageDirectory(); File newFile = new File(root.getAbsolutePath()+File.separator+editText1.getText()); try { if(!newFile.exists()){ newFile.createNewFile(); } FileOutputStream out = new FileOutputStream(newFile); out.write(editText2.getText().toString().getBytes()); out.close(); textview.setText("写文件成功."); } catch (Exception e) { // TODO: handle exception } } } }); } } |