android 6.0 写入SD卡的权限申请实例讲解
6.0的手机对于写入手机需要申请权限的
我做了如下处理
下面我贴出代码
packagecom.example.admin.sdapplication; importandroid.Manifest; importandroid.app.Dialog; importandroid.content.DialogInterface; importandroid.content.pm.PackageManager; importandroid.os.Build; importandroid.os.Bundle; importandroid.os.Environment; importandroid.support.annotation.NonNull; importandroid.support.v4.app.ActivityCompat; importandroid.support.v4.content.ContextCompat; importandroid.support.v7.app.AlertDialog; importandroid.support.v7.app.AppCompatActivity; importandroid.util.Log; importandroid.view.View; importandroid.widget.Button; importjava.io.BufferedReader; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileOutputStream; importjava.io.InputStreamReader; publicclassSdkActivityextendsAppCompatActivityimplementsView.OnClickListener{ privateButtonbtn_write; privateButtonbtn_read; finalintREQUEST_WRITE=1;//申请权限的请求码 @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_sdk); btn_write=(Button)findViewById(R.id.btn_write); btn_read=(Button)findViewById(R.id.btn_read); btn_read.setOnClickListener(this); btn_write.setOnClickListener(this); } privatevoidshowAlert(){ DialogalertDialog=newAlertDialog.Builder(this). setTitle("权限说明"). setMessage("我们需要这个权限给你提供存储服务"). setIcon(R.drawable.ic_launcher). setPositiveButton("确定",newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ //TODOAuto-generatedmethodstub //2、申请权限:参数二:权限的数组;参数三:请求码 ActivityCompat.requestPermissions(SdkActivity.this,newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_WRITE); } }). setNegativeButton("取消",newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ //TODOAuto-generatedmethodstub } }). create(); alertDialog.show(); } @Override publicvoidonClick(Viewview){ switch(view.getId()){ caseR.id.btn_read: Strings=readToSdCard(); Log.i("readTosdCard",s); break; caseR.id.btn_write: //判断是否6.0以上的手机不是就不用 if(Build.VERSION.SDK_INT>=23){ //判断是否有这个权限 if(ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){ //第一请求权限被取消显示的判断,一般可以不写 if(ActivityCompat.shouldShowRequestPermissionRationale(SdkActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){ Log.i("readTosdCard","我们需要这个权限给你提供存储服务"); showAlert(); }else{ //2、申请权限:参数二:权限的数组;参数三:请求码 ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_WRITE); } }else{ writeToSdCard(); } }else{ writeToSdCard(); } break; } } //写数据 publicvoidwriteToSdCard(){ //1、判断sd卡是否可用 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //sd卡可用 //2、获取sd卡路径 FilesdFile=Environment.getExternalStorageDirectory(); Filepath=newFile(sdFile,"a.txt");//sd卡下面的a.txt文件参数前面是目录后面是文件 try{ FileOutputStreamfileOutputStream=newFileOutputStream(path); fileOutputStream.write("hello".getBytes()); }catch(Exceptione){ e.printStackTrace(); } } } //读数据 publicStringreadToSdCard(){ StringBuilderstringBuilder=null; if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ FilesdFile=Environment.getExternalStorageDirectory(); Filepath=newFile(sdFile,"a.txt"); stringBuilder=newStringBuilder(); try{ FileInputStreamfileInputStream=newFileInputStream(path); BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(fileInputStream)); Stringstr; while((str=bufferedReader.readLine())!=null){ stringBuilder.append(str); } }catch(Exceptione){ e.printStackTrace(); } } returnstringBuilder.toString(); } //判断授权的方法授权成功直接调用写入方法这是监听的回调 //参数上下文授权结果的数组申请授权的数组 @Override publicvoidonRequestPermissionsResult(intrequestCode,@NonNullString[]permissions,@NonNullint[]grantResults){ super.onRequestPermissionsResult(requestCode,permissions,grantResults); if(requestCode==REQUEST_WRITE&&grantResults[0]==PackageManager.PERMISSION_GRANTED){ writeToSdCard(); } } }
其实就是判断是不是6.0的手机再进行申请权限的处理利用申请权限的回调机制直接在权限申请下来之后调用写入处理
注释很清楚你们看看代码就好了(⊙o⊙)!
另外需要注意的一点我们也处理了第一次用户拒绝权限,第二次需要请求时,给用户提示权限作用
//第一请求权限被取消显示的判断,一般可以不写 if(ActivityCompat.shouldShowRequestPermissionRationale(SdkActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){ Log.i("readTosdCard","我们需要这个权限给你提供存储服务"); showAlert(); }else{ //2、申请权限:参数二:权限的数组;参数三:请求码 ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_WRITE); }
以上这篇android6.0写入SD卡的权限申请实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。