为了更好的存取应用程序的大文件数据,应用程序需要读. 写SD卡上的文件。SD卡大大扩充手机的存储能力。
操作SD首先要加权限:
<!--在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- 从SDCard读取数据权限 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
读写SD卡上的文件请按以下三步进行:
(1)调用Environment的getExternalStorageState()方法判断手机上是否插入了SD卡,并且应用程序具有读写SD卡的权限;
(2)调用Environment的getExternalStorageDirectory()方法来获取外部存储器,也就是SD卡的目录;
(3)使用FileInputStream FileOutputStream FileReader或者FileWriter读写SD卡里面的文件;
下面以简单实例说明:
1.XML代码:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <Button 12 android:id="@+id/button1" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:layout_alignParentTop="true" 16 android:layout_centerHorizontal="true" 17 android:text="向SD中写入数据" /> 18 19 <Button 20 android:id="@+id/button2" 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:layout_alignLeft="@+id/button1" 24 android:layout_below="@+id/button1" 25 android:text="从SD卡中读取数据" /> 26 27 </RelativeLayout>
2.java代码:
1 package com.example.filesd; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileOutputStream; 7 import java.io.InputStreamReader; 8 import android.os.Bundle; 9 import android.os.Environment; 10 import android.app.Activity; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.widget.Button; 14 import android.widget.Toast; 15 16 public class MainActivity extends Activity { 17 private Button button; 18 private Button button2; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 button = (Button) this.findViewById(R.id.button1); 24 button2 = (Button) this.findViewById(R.id.button2); 25 button.setOnClickListener(new OnClickListener() { 26 @Override 27 public void onClick(View arg0) { 28 writeSDcard("Hello,恭喜您数据读取了!"); 29 } 30 }); 31 button2.setOnClickListener(new OnClickListener() { 32 @Override 33 public void onClick(View arg0) { 34 readSDcard(); 35 36 } 37 }); 38 } 39 40 // 向SD卡写入数据 41 private void writeSDcard(String str) { 42 try { 43 // 判断是否存在SD卡 44 if (Environment.getExternalStorageState().equals( 45 Environment.MEDIA_MOUNTED)) { 46 // 获取SD卡的目录 47 File sdDire = Environment.getExternalStorageDirectory(); 48 FileOutputStream outFileStream = new FileOutputStream( 49 sdDire.getCanonicalPath() + "/test.txt"); 50 outFileStream.write(str.getBytes()); 51 outFileStream.close(); 52 Toast.makeText(this, "数据保存到text.txt文件了", Toast.LENGTH_LONG) 53 .show(); 54 } 55 } catch (Exception e) { 56 e.printStackTrace(); 57 } 58 } 59 60 61 // 从SD卡中读取数据 62 private void readSDcard() { 63 StringBuffer strsBuffer = new StringBuffer(); 64 try { 65 // 判断是否存在SD 66 if (Environment.getExternalStorageState().equals( 67 Environment.MEDIA_MOUNTED)) { 68 File file = new File(Environment.getExternalStorageDirectory() 69 .getCanonicalPath() + "/test.txt"); 70 // 判断是否存在该文件 71 if (file.exists()) { 72 // 打开文件输入流 73 FileInputStream fileR = new FileInputStream(file); 74 BufferedReader reads = new BufferedReader( 75 new InputStreamReader(fileR)); 76 String st = null; 77 while ((st = reads.readLine()) != null) { 78 strsBuffer.append(st); 79 } 80 fileR.close(); 81 } else { 82 Toast.makeText(this, "该目录下文件不存在", Toast.LENGTH_LONG).show(); 83 } 84 } 85 } catch (Exception e) { 86 e.printStackTrace(); 87 } 88 Toast.makeText(this, "读取到的数据是:" + strsBuffer.toString() + "", 89 Toast.LENGTH_LONG).show(); 90 } 91 }
时间: 2024-10-24 15:30:34