错误:
W/System.err: java.io.FileNotFoundException: /mnt/sdcard/007.test: open failed: EACCES (Permission denied) W/System.err: ? ? at libcore.io.IoBridge.open(IoBridge.java:496) W/System.err: ? ? at java.io.FileOutputStream.<init>(FileOutputStream.java:235) W/System.err: ? ? at java.io.FileOutputStream.<init>(FileOutputStream.java:157) W/System.err: ? ? at com.example.sdcardtest.MainActivity.save(MainActivity.java:41) W/System.err: ? ? at com.example.sdcardtest.MainActivity$1.onClick(MainActivity.java:32) W/System.err: ? ? at android.view.View.performClick(View.java:7284) W/System.err: ? ? at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) W/System.err: ? ? at android.view.View.performClickInternal(View.java:7258) W/System.err: ? ? at android.view.View.access$3600(View.java:831) W/System.err: ? ? at android.view.View$PerformClick.run(View.java:27948) W/System.err: ? ? at android.os.Handler.handleCallback(Handler.java:900) W/System.err: ? ? at android.os.Handler.dispatchMessage(Handler.java:103) W/System.err: ? ? at android.os.Looper.loop(Looper.java:219) W/System.err: ? ? at android.app.ActivityThread.main(ActivityThread.java:8401) W/System.err: ? ? at java.lang.reflect.Method.invoke(Native Method) W/System.err: ? ? at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) W/System.err: ? ? at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1102) W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied) W/System.err: ? ? at libcore.io.Linux.open(Native Method) W/System.err: ? ? at libcore.io.ForwardingOs.open(ForwardingOs.java:167) W/System.err: ? ? at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252) W/System.err: ? ? at libcore.io.ForwardingOs.open(ForwardingOs.java:167) W/System.err: ? ? at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8250) W/System.err: ? ? at libcore.io.IoBridge.open(IoBridge.java:482) W/System.err: ?? ?... 16 more
MainActivity代码:
package com.example.sdcardtest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity {
private EditText edit;
private Button mBtn;
final String FILE_NAME = "/mnt/sdcard/007.test";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = findViewById(R.id.edit);
mBtn = findViewById(R.id.btn);
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 动态请求,设置请求码为1
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 1){
for (int i = 0; i < permissions.length; i++) {
if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
String input = edit.getText().toString();
save(input);
}else {
Toast.makeText(this, "权限"+permissions[i]+"申请失败!!!", Toast.LENGTH_SHORT).show();
}
}
}
}
public void save(String inputText) {
FileOutputStream out = null;
BufferedWriter writer = null;
try {
FileOutputStream fout = new FileOutputStream(FILE_NAME, true);
BufferedOutputStream bout = new BufferedOutputStream(fout);
bout.write(Integer.parseInt(inputText));
bout.flush();
bout.close();
Toast.makeText(MainActivity.this,"保存成功!!!",Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
? ?在动态申请下,依然还是报错。让我非常苦恼!在网上找了好久好久,终于找到了解决办法。
就是在Manifest文件下的application下加入下面代码就解决了
android:requestLegacyExternalStorage="true"
<application
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.DealEvent">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
参考文章:解决各版本安卓读写SD卡的问题-java.io.IOException: Operation not permitted问题_失落夏天的博客-CSDN博客_安卓11sd卡无法写入
|