W/System.err: java.io.FileNotFoundException: /mnt/sdcard/007.test: open failed: EACCES这个异常终于被我修复了

错误:

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卡无法写入

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请检查 以下错误W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Pictures/Screenshots/Screenshot_20230622_152002.jpg: open failed: EACCES (Permission denied) W/System.err: at libcore.io.IoBridge.open(IoBridge.java:575) W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:160) W/System.err: at okio.Okio__JvmOkioKt.source(JvmOkio.kt:178) W/System.err: at okio.Okio.source(Unknown Source:1) W/System.err: at okhttp3.RequestBody$Companion$asRequestBody$1.writeTo(RequestBody.kt:167) W/System.err: at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.kt:157) W/System.err: at okhttp3.MultipartBody.writeTo(MultipartBody.kt:93) W/System.err: at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:59) W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:34) W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83) W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76) W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) W/System.err: at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201) W/System.err: at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517) W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) W/System.err: at java.lang.Thread.run(Thread.java:930) 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:567) W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:273) W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:567) W/System.err: at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8807) W/System.err: at libcore.io.IoBridge.open(IoBridge.java:561) W/System.err: ... 21 more
07-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值