一、介绍
这是一个很简单的Demo,只是点击按钮后往SDCard中写入一个文本。
二、案例实现
1.搭建UI(代码如下)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="测试写入SDcord"
android:onClick="OnClickButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.书写权限工具类(代码如下)
package com.hiscene.testsd;
import android.content.pm.PackageManager;
import android.os.Build;
public class PermisionUtils {
//检查用户权限
public static boolean CheckPermissions(MainActivity activity, int code, String[] permissons) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
boolean needPermission = false;
for (String permisson : permissons) {
if (activity.checkSelfPermission(permisson) != PackageManager.PERMISSION_GRANTED) {
needPermission = true;
}
}
//用户没有权限
if (needPermission) {
activity.requestPermissions(permissons, code);
return false;
} else {
return true;
}
}
else { return true; }
}
}
3.书写业务逻辑(代码如下)
package com.hiscene.testsd;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
public class MainActivity extends AppCompatActivity {
//授权请求码
private static final int PERMISSIONS_CODE = 656;
private MainActivity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity=MainActivity.this;
}
public void OnClickButton(View view) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(activity, "SDCard就绪", Toast.LENGTH_LONG).show();
//动态申请权限
String[] permissons={Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (PermisionUtils.CheckPermissions(activity,PERMISSIONS_CODE,permissons)) Write();
}
}
private void Write()
{
File ext = Environment.getExternalStorageDirectory();//SD卡目录
File f2 = new File(ext, "user.info");
try {
OutputStream out = new FileOutputStream(f2);
out.write("helloworld".getBytes());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//授权回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSIONS_CODE) {
for (int i = 0; i < permissions.length; i++) {
String s = permissions[i];
if (grantResults[i] != PERMISSION_GRANTED) {
//用户拒绝授权
Toast.makeText(activity, "写入Sdcord失败,需要读取和写入权限!", Toast.LENGTH_LONG).show();
} else {
Write();
}
}
}
}
}