实现功能:
写入SD卡:输入文件名称和文件内容 点击写入按钮 生成123.txt文件
从SD卡读取:输入文件名称,点击读取按钮 取到到文件内容
/**
* 把文件内容保存到SD卡
* @param filename 文件名称
* @param content 文件内容
* @throws Exception
*/
public void fileContentSaveToSD(String filename,String content)throws Exception{
File file=new File(Environment.getExternalStorageDirectory(),filename);
FileOutputStream outputStream=new FileOutputStream(file);
outputStream.write(content.getBytes());
outputStream.close();
}
/**
* 从SD卡读取文件内容
* @param filename 文件名称
* @return
* @throws Exception
*/
public String fileContentReadFromSD(String filename) throws Exception{
File file=new File(Environment.getExternalStorageDirectory(),filename);
FileInputStream inputStream=new FileInputStream(file);
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
int len=0;
byte[] buffer=new byte[1024];
while ((len=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
byte[] data=outputStream.toByteArray();
outputStream.close();
return new String(data);
}
1.SD卡的路径获取方法:Environment.getExternalStorageDirectory()
2.SD卡的读写需要配置权限
1)在androidmanifest.xml 配置
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2)安卓6.0后SD卡的读写的读写是危险权限,需要动态申请,代码如下:
bt_writeToSD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { //判断是否有权限
writeToSd();
}else{
//没有权限动态申请
requestPermission(PERMISSION_WRITE_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
}
});
bt_readFromSD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
readSD();
}else{
requestPermission(PERMISSION_WRITE_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
}
});
3)关于权限动态申请的封装代码如下:
public class BaseActivity extends AppCompatActivity {
public static final int PERMISSION_CALL_PHONE=0x01;
public static final int PERMISSION_WRITE_EXTERNAL_STORAGE=0x02;
public static final int PERMISSION_READ_EXTERNAL_STORAGE=0x03;
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
}
/**
* 判断是否拥有某项权限
* @param permissions 权限
* @return
*/
public boolean hasPermission(String... permissions){
for(String permission:permissions){
if(ContextCompat.checkSelfPermission(this,permission)!= PackageManager.PERMISSION_GRANTED){
return false;
}
}
return true;
}
/**
* 申请权限
* @param code 权限申请成功后的反馈代码
* @param permissions 权限
*/
public void requestPermission(int code,String... permissions){
ActivityCompat.requestPermissions(this,permissions,code);
}
/**
* 默认拨打电话的权限
*/
public void doCallPhone(){
}
/**
* 申请权限后,处理的任务
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case PERMISSION_CALL_PHONE:
doCallPhone();
break;
default: break;
}
}
}