在做一个写入sd卡文件的app时无法获取读写权限
代码如下
package com.example.timemachine;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private String fileName;
// 记录目录路径
private String fileDir;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//android 6.0以上
//进入程序,申请读写权限
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
}
}
}
public void TimeC1(View view){
@SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
String y= format.format(new Date());
TextView textView1=(TextView) findViewById(R.id.TimeY);
textView1.setText(y);
}
public void TimeC2(View view){
@SuppressLint("SimpleDateFormat") SimpleDateFormat format1=new SimpleDateFormat("ddHHmm");
String d=format1.format(new Date());
TextView textView2=(TextView) findViewById(R.id.TimeD);
textView2.setText(d);
}
public void TimeC(View view){
String y1;
String d1;
String date;
TextView textView1=(TextView) findViewById(R.id.TimeY);
TextView textView2=(TextView) findViewById(R.id.TimeD);
y1=textView1.getText().toString();
d1=textView2.getText().toString();
String yd;
yd=(y1+"\ndayTime "+d1);
String head;
{head=("default 0\nmres 24\nphoto_size 0\nsplit 222\nyearMonth");}
date=(head+" "+yd);
TextView textView3=(TextView) findViewById(R.id.textView);
textView3.setText(date);
}
private void createFile(){
String mStrPath = Environment.getExternalStorageDirectory().getPath() + "/default.ini";
File mFile=new File(mStrPath);
TextView textView=(TextView) findViewById(R.id.textView2);
if (mFile.exists()){
mFile.delete();
}
try {
mFile.createNewFile();
textView.setText("文件创建成功");
}catch (IOException e){
e.printStackTrace();
textView.setText("文件创建失败");
}
}
public void writText(String fileName, String date){
String y1;
String d1;
TextView textView1=(TextView) findViewById(R.id.TimeY);
TextView textView2=(TextView) findViewById(R.id.TimeD);
y1=textView1.getText().toString();
d1=textView2.getText().toString();
String yd;
yd=(y1+"\ndayTime "+d1);
String head;
{head=("default 0\nmres 24\nphoto_size 0\nsplit 222\nyearMonth");}
date=(head+" "+yd);
try {
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(fileName,true),"gbk");
osw.write(date);
osw.close();
}catch (IOException e){
e.printStackTrace();
}
}
public void YingY(View view){
String y1;
String d1;
String date;
TextView textView1=(TextView) findViewById(R.id.TimeY);
TextView textView2=(TextView) findViewById(R.id.TimeD);
y1=textView1.getText().toString();
d1=textView2.getText().toString();
String yd;
yd=(y1+"\ndayTime "+d1);
String head;
{head=("default 0\nmres 24\nphoto_size 0\nsplit 222\nyearMonth");}
date=(head+" "+yd);
createFile();
}
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/206970695876142.png "#left")