快速解决设置Android 23.0以上版本对SD卡的读写权限无效的问题
转 https://www.jb51.net/article/144939.htm
今天小编就为大家分享一篇快速解决设置Android 23.0以上版本对SD卡的读写权限无效的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
对Android的SD卡进行读取权限设置时:
1
2
|
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name= "android.permission.READ_EXTERNAL_STORAGE" /> |
结果报错:
依然提示我没有权限,于是百度说是版本问题,23.0版本(笔者的版本是25.0)以上的不仅仅要设置上面的权限,还要在对SD卡有读写操作的地方授权,下面是公共类:
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
|
public class PermisionUtils { // Storage Permissions private static final int REQUEST_EXTERNAL_STORAGE = 1 ; private static String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; /** * Checks if the app has permission to write to device storage * If the app does not has permission then the user will be prompted to * grant permissions * * @param activity */ public static void verifyStoragePermissions(Activity activity) { // Check if we have write permission int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (permission != PackageManager.PERMISSION_GRANTED) { // We don't have permission so prompt the user ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE); } } } |
然后直接在需要授权的地方调用:
1
2
|
//检测读写权限 PermisionUtils.verifyStoragePermissions( this ); |
程序运行的时候,会询问是否授权
点击授权即可。
以上这篇快速解决设置Android 23.0以上版本对SD卡的读写权限无效的问题就是小编分享给大家的全部内容了,希望能给大家一个参考
本人练习一个例子源码:
package com.example.datastrorage;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileActivity extends AppCompatActivity {
private EditText mEtData;
private Button mBtSave;
private TextView mTvShow;
private Button mBtShow;
final String mFilename="data.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
mEtData = findViewById(R.id.et_file_data);
mBtSave = findViewById(R.id.bt_file_save);
mTvShow = findViewById(R.id.tv_file_show);
mBtShow = findViewById(R.id.bt_file_show);
mBtSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveData(mEtData.getText().toString());
}
});
mBtShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTvShow.setText(readData(mFilename));
}
});
//检测读写权限
PermisionUtils.verifyStoragePermissions(this);
}
//保存数据
private void saveData(String data){
FileOutputStream fileOutputStream = null;
try {
//内部文件访问
//fileOutputStream = openFileOutput(mFilename, MODE_PRIVATE);
//外部文件访问
File dir=new File(Environment.getExternalStorageDirectory(),"save");
if(!dir.exists()){
dir.mkdirs();
}
File file=new File(dir,mFilename);
if(!file.exists()){
file.createNewFile();
}
fileOutputStream = new FileOutputStream(file);
//end-外部文件访问
fileOutputStream.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//读取数据
private String readData(String mFilename){
String data="";
FileInputStream fileInputStream = null;
try {
//内部文件访问
//fileInputStream = openFileInput(mFilename);
//外部文件访问
String path="";
path=Environment.getExternalStorageDirectory().getAbsolutePath();
Log.d("FileActivity",path);
File file=new File(path+File.separator+"save",mFilename);
fileInputStream = new FileInputStream(file);
//end-外部文件访问
byte[] buf = new byte[1024];
int len;
StringBuilder sb = new StringBuilder("");
while ((len = fileInputStream.read(buf)) > 0) {
sb.append(new String(buf,0,len));
}
data=sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return data;
}
}
其它参考例子: