当前位置: 首页 > 知识库问答 >
问题:

java.io.FileNotFoundException:/sdcard/testwrite.txt:open失败:EACCES(拒绝权限)

唐恺
2023-03-14
E: Unknown bits set in runtime_flags: 0x8000
E: --> WRITE_EXTERNAL_STORAGE=true
E: --> READ_EXTERNAL_STORAGE=true
E: ====================================================================
E: --> TEST: Writing.. /sdcard/testwrite.txt
E: --> Writing..
E: --> Error..
E: java.io.FileNotFoundException: /sdcard/testwrite.txt: open failed: EACCES (Permission denied)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp.TestWrite">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        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/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
package com.myapp.TestWrite;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.util.Log;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        Log.wtf( "TAG", "--> WRITE_EXTERNAL_STORAGE=" + hasPermission( Manifest.permission.WRITE_EXTERNAL_STORAGE ) );
        Log.wtf( "TAG", "--> READ_EXTERNAL_STORAGE=" + hasPermission( Manifest.permission.READ_EXTERNAL_STORAGE ) );

        if( Build.VERSION.SDK_INT >= 23 )
        if( hasPermission( Manifest.permission.WRITE_EXTERNAL_STORAGE ) )
        if( hasPermission( Manifest.permission.READ_EXTERNAL_STORAGE ) ) {
            writeTest( "/sdcard/testwrite.txt" );
            return;
        }

        Log.wtf( "TAG", "--> Request permission\n" );
        ActivityCompat.requestPermissions(
                this,
                new String[]{
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                },
                1
        );
    }

    void writeTest( String fn ) {
        Log.wtf( "TAG", "====================================================================" );
        Log.wtf( "TAG", "--> TEST: Writing.. " + fn );
        File file = new File( fn );
        try {
            Log.wtf( "TAG", "--> Writing.." );;
            FileWriter fw = new FileWriter(file, false);
            fw.write( "Test Doc !!!!!" );
            fw.close();
            Log.wtf( "TAG", "--> Success.." );
        } catch( IOException e) {
            e.printStackTrace();
            Log.wtf( "TAG", "--> Error.." );
            Log.wtf( "TAG", e.toString() );
        }
    }

    public boolean hasPermission( String strPerm ) {
        if ( ContextCompat.checkSelfPermission( this, strPerm ) == PackageManager.PERMISSION_GRANTED )
            return true;
        return false;
    }

    @Override
    public void onRequestPermissionsResult( int requestCode, String[] permissions, int[] grantResults ) {
        Log.wtf( "TAG", ":onRequestPermissionsResult\n" );
        switch ( requestCode ) {
            case 1:
            {
                // If request is cancelled, the result arrays are empty.
                if ( grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED ) {
                    Log.wtf( "TAG", "--> Permission granted.\n" );
                    finish();
                    startActivity( getIntent() );
                }
                else {
                    Log.wtf( "TAG", "--> Permission denied. Quitting.\n" );
                    finish();
                }
            }
        }
    }
}

共有1个答案

鄢飞鸾
2023-03-14

您在Android10中运行的应用程序没有在清单中包含legacy support属性。按照以下建议编辑您的舱单:

<manifest ... >
  <!-- This attribute is "false" by default on apps targeting
       Android 10 or higher. -->
  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>
</manifest>

(资料来源)

注意,这只是一个临时的修复,因为它将默认为false随着下一个android版本的发布。如果这些文件是应用程序专用的,请考虑使用getExternalFilesDir()动态获取路径。在开发人员的网站上阅读更多关于范围存储更改的信息。

 类似资料: