读写SD卡上的文件
通过Context的openFileInput、openFileOutput来打开文件输入流、输出流时,程序打开的都是应用程序的数据文件夹里的文件,其存储的文件大小可能都比较有限——手机内存所限。
SD卡可以更好的存、取应用程序的大小文件数据。SD卡可以大大扩充手机的存储能力。
读、写SD卡上的文件的操作步骤如下:
1.调用Environment的getExternalStorageState()方法判断手机上是否插入了SD卡, 并且应用程序具有读写SD卡的权限。例如使用如下代码:
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
2.调用Environment的getExternalStorageDirectory()方法来获取外部存储器,也就是SD卡的目录;
3.使用FileInputStream、FileOutputStream、FileReader/FileWriter读、写SD卡里面的文件。
注意:
应用程序读、写SD卡的文件有如下两个注意点:
1.手机上应该已插入SD卡。对于模拟器来说,可通过mksdcard命令来创建虚拟存储卡。
2.为了读、写SD卡上的数据,必须在应用程序的清单文件(AndroidMainfest.xml)中添加读写SD卡的权限.
Eg:
实例如下:
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 | 布局文件==》 <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= ".MainActivity" > <EditText android:id= "@+id/edit1" android:layout_width= "match_parent" android:layout_height= "wrap_content" /> <Button android:id= "@+id/btnWrite" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "write" /> <EditText android:id= "@+id/edit2" android:layout_width= "match_parent" android:layout_height= "wrap_content" /> <Button android:id= "@+id/btnRead" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "read" /> </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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | package com.example.mysdcard; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.RandomAccessFile; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { final String FILE_NAME = "/crazyit" ; EditText Edit1; EditText Edit2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnWrite = (Button) this .findViewById(R.id.btnWrite); Button btnRead = (Button) this .findViewById(R.id.btnRead); Edit1 = (EditText) this .findViewById(R.id.edit1); Edit2 = (EditText) this .findViewById(R.id.edit2); btnWrite.setOnClickListener( new MyButtonClick()); btnRead.setOnClickListener( new MyButtonClick()); } private class MyButtonClick implements OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnWrite: Log.i( "swg" , "write==" + Edit1.getText()); write(Edit1.getText().toString()); Edit1.setText( "" ); break ; case R.id.btnRead: Log.i( "swg" , "read==" + read()); Edit2.setText(read()); break ; } } private String read() { try { File file = new File(Environment.getExternalStorageDirectory(), FILE_NAME); BufferedReader br = new BufferedReader( new FileReader(file)); String readline = "" ; StringBuffer sb = new StringBuffer(); while ((readline = br.readLine()) != null ) { System. out .println( "readline:" + readline); sb.append(readline); } br.close(); System. out .println( "读取成功:" + sb.toString()); } catch (Exception e) { e.printStackTrace(); } return null ; } private void write(String context) { try { Log.i( "swg" , "write" ); // 如果手机插入SD卡,且应用程序具有访问权限 boolean value =Environment.getExternalStorageState(). equals (Environment.MEDIA_MOUNTED); Log.i( "swg" , "value==" + value); if (Environment.getExternalStorageState(). equals (Environment.MEDIA_MOUNTED)) { Log.i( "swg" , "sd exist ok" ); // 获取SD卡对应的存储目录 File sdcardDir = Environment.getExternalStorageDirectory(); String filePath = sdcardDir.getCanonicalPath() + FILE_NAME; Log.i( "swg" , filePath); File targerFile = new File(filePath); // 以拟定文件创建RandomAccessFile对象 RandomAccessFile raf = new RandomAccessFile(targerFile, "rw" ); raf.seek(targerFile.length()); raf.write(context.getBytes()); raf.close(); } else Log.i( "swg" , "sd is not op" ); } catch (Exception e) { e.printStackTrace(); } } } @Override public 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 ; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本