记一次在android的学习中遇到的SD卡权限问题。最开始题主本来是想做一个小的demo来操作SD卡。如图所示:
没想到在开发的过程中遇到了权限问题。
首先贴一下activity的代码。其实就是三个按钮,绑定三个事件。
```java
package com.example.demo;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.io.IOException;
/**
* @Author godv
* Date on 2020/4/14 22:02
*/
public class FileActivity extends AppCompatActivity {
private EditText editname;
private EditText editdetail;
private Button btnsave;
private Button btnclean;
private Button btnread;
private Context mContext;
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE" };
/*
* android 动态权限申请
* */
public static void verifyStoragePermissions(Activity activity) {
try {
//检测是否有写的权限
int permission = ActivityCompat.checkSelfPermission(activity,
"android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
// 没有写的权限,去申请写的权限,会弹出对话框
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file_layout);
verifyStoragePermissions(this);
mContext = getApplication();
editdetail = (EditText) findViewById(R.id.editdetail);
editname = (EditText) findViewById(R.id.editname);
btnclean = (Button) findViewById(R.id.btnclean);
btnsave = (Button) findViewById(R.id.btnsave);
btnread = (Button) findViewById(R.id.btnread);
//清空
btnclean.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editdetail.setText("");
editname.setText("");
}
});
//保存
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FileHelper fHelper = new FileHelper(mContext);
String filename = editname.getText().toString();
String filedetail = editdetail.getText().toString();
try {
fHelper.savFileToSD(filename, filedetail);
Toast.makeText(getApplicationContext(), "数据写入成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "数据写入失败", Toast.LENGTH_SHORT).show();
}
}
});
btnread.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String detail = "";
FileHelper fHelper2 = new FileHelper(getApplicationContext());
String fname = editname.getText().toString();
try {
detail = fHelper2.readFromSD(fname);
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), detail, Toast.LENGTH_SHORT).show();
}
});
}
}
```
其中FileHelper的代码如下:
```java
package com.example.demo;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @Author godv
* Date on 2020/4/14 22:13
*/
public class FileHelper {
private Context mContext;
public FileHelper() {
}
public FileHelper(Context mContext) {
super();
this.mContext = mContext;
}
/**
* @param filename
* @param fileContent
* @throws Exception
*/
public void savFileToSD(String filename, String fileContent) throws Exception {
//如果手机已插入sd卡,且app具有读写sd卡的权限
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;
File file=new File(filename);
FileOutputStream output = new FileOutputStream(file);
output.write(fileContent.getBytes());
//将String字符串以字节流的形式写入到输出流中
output.close();
//关闭输出流
} else{
Toast.makeText(mContext, "SD卡不存在或者不可读写", Toast.LENGTH_SHORT).show();
}
}
//读取SD卡中文件的方法
//定义读取文件的方法:
public String readFromSD(String filename) throws IOException {
StringBuilder sb = new StringBuilder("");
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;
//打开文件输入流
File file=new File(filename);
FileInputStream input = new FileInputStream(file);
byte[] temp = new byte[1024];
int len = 0;
//读取文件内容:
while ((len = input.read(temp)) > 0) {
sb.append(new String(temp, 0, len));
}
//关闭输入流
input.close();
}
return sb.toString();
}
}
```
但是在android 6+后操作SD卡有动态权限问题。我们需要在Android中添加动态权限 也就是上述activity中的verifyStoragePermissions方法。当然在Androidmanifast中配置也是必不可少的。
<!-- sd读写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
但是问题还是没有解决。在我们获取操作流的时候。
FileOutputStream output = new FileOutputStream(file);
还是会出现报错。
出现报错的原因是由于android 10中文件读写新特性。还需要在Androidmanifast的application节点中加入android:requestLegacyExternalStorage="true"。最后问题得到解决。
参考文档: