打开失败:EACCES(权限被拒绝)错误

时间:2020-04-04 12:51:39

标签: android permissions android-asynctask

我正在后台线程中从Internet下载文件。我声明了权限,还要求在运行时授予权限,但仍然出现此异常。我进行了很多搜索,得到的只是询问运行时权限,我这样做了 这是我的onclick函数

    String[] permission ={Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ActivityCompat.requestPermissions(MainActivity.this, permission, 1);
        }
    } else
    {
        new DownloadFileFromURL().execute(url,name);
    }

这是我的异步任务

    class DownloadFileFromURL extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Resources res =getResources();
            Drawable drawable = res.getDrawable(R.drawable.circular);
            mProgress.setProgress(0);   // Main Progress
            mProgress.setSecondaryProgress(100); // Secondary Progress
            mProgress.setMax(100); // Maximum Progress
            mProgress.setProgressDrawable(drawable);
        }

        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {
                URL url = new URL(f_url[0]);
                String name= f_url[1];
                URLConnection conection = url.openConnection();
                conection.connect();

                int lenghtOfFile = conection.getContentLength();
                InputStream input = new BufferedInputStream(url.openStream(),
                        8192);
                OutputStream output = new FileOutputStream(Environment
                        .getExternalStorageDirectory().toString()
                        + name+".apk");

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress("" + (int) ((total * 100) / lenghtOfFile));

                    output.write(data, 0, count);
                }

                output.flush();

                output.close();
                input.close();

            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }

            return null;
        }

        protected void onProgressUpdate(final String... progress) {

            new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    while (pStatus < 100) {
                        pStatus = Integer.parseInt(progress[0]);

                        handler.post(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                mProgress.setProgress(pStatus);
                                tv.setText(pStatus + "%");

                            }
                        });
                    }
                }
            }).start();
        }


        @Override
        protected void onPostExecute(String file_url) {

        }

    }

我认为还有一些其他问题,我已经声明了读取外部存储并在清单中写入外部存储

1 个答案:

答案 0 :(得分:0)

您应该替换

OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+ name+".apk");

在您的代码中写上

OutputStream output = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), name+".apk"));