針對Android訪問sd卡里面的音訊檔案

qq_43263637發表於2020-12-03

針對Android 8.0版本之後出現訪問sd卡許可權的問題,類似java.io.FileNotFoundException: /storage/emulated/0/data.txt: open failed: EACCES (Permission denied)

一:首先檢視自己AndroidMainifest

(1)看看自己有沒有這樣的程式碼

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

(2)application裡面

android:requestLegacyExternalStorage="true"

二:看自己的主程式裡面又沒有設定訪問許可權的函式

// Storage Permissions
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    public static void verifyStoragePermissions (Activity activity) {
        // Check if we have write permission
        int permission =
                ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            System.out.println("Sorry");
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }

相關文章