前面写了一篇Android对文件的基本读写操作,只是向手机自身的存储空间写入文件,但是手机自身的存储空间有限,这时就要考虑向SDCard中存入文件,比如视频,音频和大量的图片。在底层代码的实现上两者没有什么区别,不过想要往SDCard中写入和读取文件首选必须在AndroidManifest.xml 注册权限:
1 2 3 4 | <!-- 加入在SDCard中创建与删除文件的权限 --> < uses -permission android:name = "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- 加入在SDCard中写入数据的权限 --> < uses -permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" /> |
FileActivity.java 程序主要实现类
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 | package net.androidla.file; ? import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ? import net.androidla.service.FileService; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; ? public class FileActivity extends Activity { ???? private EditText filenameTxt; ???? private EditText fileContentTxt; ???? private TextView result; ???? private Button btnSave; ???? private Button btnShow; ???? @Override ???? public void onCreate(Bundle savedInstanceState) { ???????? super .onCreate(savedInstanceState); ???????? setContentView(R.layout.main); ???????? filenameTxt = (EditText) findViewById(R.id.filenameTxt); ???????? fileContentTxt = (EditText) findViewById(R.id.fileContentTxt); ???????? btnSave = (Button) findViewById(R.id.btnSave); ???????? btnShow = (Button) findViewById(R.id.btnShow); ???????? result = (TextView) findViewById(R.id.result); ? ???????? btnSave.setOnClickListener(listener); ???????? btnShow.setOnClickListener(listener); ???? } ???? private View.OnClickListener listener = new OnClickListener() { ???????? @Override ???????? public void onClick(View v) { ???????????? Button button = (Button) v; ???????????? String filename = filenameTxt.getText().toString(); ???????????? File file = new File(Environment.getExternalStorageDirectory(), filename); ???????????? Log.i( "log" , file.getAbsolutePath()); ???????????? switch (button.getId()) { ???????????? case R.id.btnSave: ???????????????? if (filename != null && !filename.equals( "" )) { ???????????????????? try { ???????????????????????? if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { ???????????????????????????? FileService.save( new FileOutputStream(file), fileContentTxt.getText().toString()); ???????????????????????????? Toast.makeText(FileActivity. this , R.string.success, Toast.LENGTH_SHORT).show(); ???????????????????????? } else { ???????????????????????????? Toast.makeText(FileActivity. this , "sdcard不存在或有写保护" , Toast.LENGTH_SHORT).show(); ???????????????????????? } ???????????????????? } catch (Exception e) { ???????????????????????? e.printStackTrace(); ???????????????????? } ???????????????? } else { ???????????????????? Toast.makeText(FileActivity. this , R.string.fail, Toast.LENGTH_SHORT).show(); ???????????????? } ???????????????? break ; ???????????? case R.id.btnShow: ???????????????? try { ???????????????????? String resultTxt = FileService.read( new FileInputStream(file)); ???????????????????? result.setText(resultTxt); ???????????????? } catch (Exception e) { ???????????????????? Toast.makeText(FileActivity. this , "读取失败" , Toast.LENGTH_SHORT).show(); ???????????????????? e.printStackTrace(); ???????????????? } ???????????????? break ; ???????????? } ???????? } ???? }; } |