一,说明
- 应用运行用到的数据文件可以保存到SD卡中
- 文件类型:任意
- 数据保存路径:
- 路径一:/storage/sdcard/Android/data/packageName/files/
- 此路径其他应用可以访问,应用卸载时删除
- 路径二:/storage/sdcark/xxx
- 此路径其他应用可以访问,应用卸载时不删除
- 必须保证sd卡挂载在手机上才能读写,否则不能操作
二,API
操作SD卡的工具类Environment
Environment.getExternalStorageState()
得到SD卡状态,可挂载状态:Environment.MEDIA_MOUNTEDEnvironment.getExternalStorageDirectory()
得到SD卡路径
路径一
context.getExternalFilesDir(String type)
得到/storage/sdcard/Android/data/packageName/files(type=null)
当type取其他常量可在files文件夹下建立不同子文件夹,详见源码
SD卡的权限
android.permission.WRITE_EXTERNAL_STORAGE
三,测试源码
/**
* 测试手机外部文件储存
*
* @author quantai.zhu
*
*/
public class SDActivity extends Activity {
private EditText et_sd_name;
private EditText et_sd_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sd);
et_sd_name = (EditText) findViewById(R.id.et_sd_name);
et_sd_content = (EditText) findViewById(R.id.et_sd_content);
}
/**
* SD卡文件保存测试
*
* @param v
* @throws IOException
*/
public void onClickSave(View v) throws IOException {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String fileName = et_sd_name.getText().toString();
String content = et_sd_content.getText().toString();
File externalFilesDir = this.getExternalFilesDir(null);
String filePath = externalFilesDir.getAbsolutePath() + "/"
+ fileName;
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(content.getBytes("utf-8"));
fos.close();
Toast.makeText(this, "传输完成", 0).show();
} else {
Toast.makeText(this, "SD卡未挂载!", 0).show();
}
}
public void onClickRead(View v) throws IOException {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String fileName = et_sd_name.getText().toString();
String filePath = this.getExternalFilesDir(null).getAbsolutePath()
+ "/" + fileName;
FileInputStream fis = new FileInputStream(filePath);
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = -1;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
baos.close();
fis.close();
String content = baos.toString();
et_sd_content.setText(content);
} else {
Toast.makeText(this, "SD卡未挂载!", 0).show();
}
}
public void onClickSave2(View v) throws IOException {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String storageDirPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
String path = storageDirPath + "/zhu";
new File(path).mkdir();
String name = et_sd_name.getText().toString();
String content = et_sd_content.getText().toString();
String filePath = path + "/" + name;
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(content.getBytes("utf-8"));
fos.close();
Toast.makeText(this, "已保存!", 0).show();
} else {
Toast.makeText(this, "SD卡未挂载!", 0).show();
}
}
public void onClickRead2(View v) throws IOException {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String storageDirPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
String filePath = storageDirPath + "/zhu/"
+ et_sd_name.getText().toString();
FileInputStream fis = new FileInputStream(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
String content = baos.toString();
baos.close();
fis.close();
et_sd_content.setText(content);
} else {
Toast.makeText(this, "SD卡未挂载!", 0).show();
}
}
}