【Android報錯】FileNotFoundException open failed:文件路徑 EPERM (Operation not permitted)外部存儲至根目錄報錯,Android外部存儲權限動態獲取問題


報錯:FileNotFoundException open failed: XXXXXXX EPERM (Operation not permitted)

查了下,大概原因是因為權限的問題。(小白學Android,Android文檔查起來還有點吃力,就直接谷歌+百度。)

但是Manifest里該加的權限都加了。還application增加了android:requestLegacyExternalStorage="true",表示沒用。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" tools:ignore="ProtectedPermissions" />

 

Android 6.0以后,Android對權限管理更嚴格了。試了幾種解決辦法,都不算我想要的。本意是想存儲到SD卡的根路徑下。

算了,先忽略了。繼續往下學習吧。

 

解決:

自定義存儲方法(原代碼)

    public void saveIMG(){
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        try {
            //獲取APP內存儲路徑,模擬器可行
//            ContextWrapper cw = new ContextWrapper(getApplicationContext());
//            File sdkPath = cw.getExternalFilesDir(Environment.DIRECTORY_PICTURES);

            //獲取public存儲路徑,模擬器可行。真機需要動態權限
//            File sdkPath=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            
            //獲取根路徑,不可行。增加動態獲取外部存儲讀取權限后,真機運行,可行。
            File sdkPath=Environment.getExternalStorageDirectory();

            Log.v("hehe","sdkPath:"+sdkPath.getPath());
            file =new File(sdkPath,"test.jpeg");
            //讀取圖片
            @SuppressLint("ResourceType") InputStream is= getResources().openRawResource(R.drawable.kelala);
            //存儲圖片
            OutputStream io=new FileOutputStream(file);

            bis=new BufferedInputStream(is);
            bos=new BufferedOutputStream(io);

            int len=0;
            byte[] buf=new byte[1024];
            while ((len=bis.read(buf))!=-1){

                bos.write(buf,0,len);
                bos.flush();
            }
            Log.v("hehe","讀取和存儲buffer");
            Toast.makeText(OutStore.this,"存儲成功!!!!!",Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


    }

調用自定義存儲方法的Activity里增加外部存儲動態權限獲取的代碼:

//需要動態授外部存儲權限。模擬器不彈動態權限授權對話框,晚點看下什么問題吧
        int REQUEST_EXTERNAL_STORAGE = 1;
        String[] PERMISSIONS_STORAGE = {"android.permission.READ_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE" };
        ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM