• 快速解决设置Android 23.0以上版本对SD卡的读写权限无效的问题


    快速解决设置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;

    }
    }

    其它参考例子:

    Android 4.4及以上版本写入外置SD卡问题

    Android中使用SDcard进行文件的读取

    Android7.0sdcard文件访问问题

    Android 8.0 中如何读取内部和外部存储以及外置SDcard

  • 相关阅读:
    eclipse常用快捷键
    Android 布局
    Android UI设计
    Android Context 是什么?
    内部类
    手写单例模式
    上课笔记:移动开发新利器-你好,Flutter
    Android Fragment
    Kotlin语言
    Android虚拟机和类加载机制
  • 原文地址:https://www.cnblogs.com/it-tsz/p/10810790.html
Copyright © 2020-2023  润新知