-1

I want to download a PDF-file from the link and open it in the pdf-reader. For this I indicated:

  • in the manifesto:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
  • in the class AppAsyncTasks.java:

    public static class FileLoadingTask extends AsyncTask<Void, Void, File> {
    
        private static final String LOG_TAG = "FileLoadingTask";
    
        public FileLoadingTask(final Context cxt,
                               final JSONOrder order,
                               final FileLoadingListener listener) {
    
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            createPdLoading(pdLoading);
            this.url = mOrder.getPreviewlink();
            this.extStorageDirectory = Environment.getExternalStorageDirectory().toString();
            this.folder = new File(extStorageDirectory, "Download");
            this.folder.mkdir();
            this.fileName = mOrder.getOrdercode() + ".pdf";
            this.file = new File(this.folder, this.fileName);
            fileLoadingListener.onBegin();
        }
    
        @Override
        protected File doInBackground(Void... params) {
            try {
                FileUtils.copyURLToFile(new URL(url), file);
            } catch (IOException e) {        
                throwable = e;
            }
            return file;
        }
    
        @Override
        protected void onPostExecute(File file) {
            super.onPostExecute(file);
            pdLoading.dismiss();
            fileLoadingListener.onEnd(file);
            if (throwable != null) {
                fileLoadingListener.onFailure(throwable);
            } else {
                fileLoadingListener.onSuccess();
            }
        }            
        public interface FileLoadingListener {
    
            void onBegin();               
            void onSuccess();              
            void onFailure(Throwable cause);                
            void onEnd(File file);                
        }
    }
    

However, writing a file to the internal media does not happen - an exception is thrown:

IOException: File 'storage/emulated/0/Download/...pdf' cannot be written to

How to get rid of this exception?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Add sufficient code . You pdf write method is missing .and post the whole crash log . – ADM Feb 14 '18 at 05:17
  • 1) post your full error log . 2) don't forget to get permission at runtime also . – Tejas Pandya Feb 14 '18 at 05:18
  • also don't forget to add Android N permission via file providers before downloading the content to destination directory. – Dhaval Patel Feb 14 '18 at 05:37
  • This exception does not fall into the error log, because it is processed further by outputting a digalog box with a message about it. But at the expense of permitting checks - this is a good topic. Thank you! – Noname Noferstname Feb 14 '18 at 06:21

1 Answers1

0

This solution is workable. I included in the code instructions for checking the write permission to external memory. As suggested by Tejas Pandya and Dhaval Patel.

To do this, my onPreExecute () method is added as follows:

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            while (checkPermissions()) {
            }
        }
        createPdLoading(pdLoading);
        ... ... ... ... ...
    }

The method checkPermissions() returns the value "true" if there is no permission to write files to external memory, and - "false", if any.

    @RequiresApi(api = Build.VERSION_CODES.M)
    private boolean checkPermissions() {
        switch (App.getCurFragContext().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            case PackageManager.PERMISSION_DENIED:
                fileLoadingListener.requestPermissions();
                return true;
            default:
                return false;
        }
    }

I added a requestPermissions() method to FileLoadingListener interface. It is called from checkPermissions() method and has the following implementation:

    @Override
    public void requestPermissions() {
        ActivityCompat.requestPermissions(
                (Activity) mActivity,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                App.MY_PERMISSIONS_REQUEST_WRITE);
    }