0

I'm having a problem when I try to download zip file from server. I'm using intentservice to download the file but I'm getting this exception

java.io.FileNotFoundException: /storage/emulated/FileTest1.zip: open failed: EACCES (Permission denied)

I try to save files on internal storage, NOT on sdcard. Here is my code

@Override
protected void onHandleIntent(Intent intent) {
    String urlDir = intent.getExtras().getString(DOWNLOAD_SERVICE_URL);
    String fileName = intent.getExtras().getString(DOWNLOAD_SERVICE_FILE_NAME);
    String filePath = intent.getExtras().getString(DOWNLOAD_SERVICE_FILE_PATH);

    try{
        URL url = new URL(urlDir);
        URLConnection connection = url.openConnection();
        connection.connect();

        BufferedInputStream input = new BufferedInputStream(url.openStream());
        String tempName = filePath + fileName;
        FileOutputStream output = new FileOutputStream(tempName);

        byte data[] = new byte[1024];

        while (input.read(data) != -1) {
            output.write(data);
        }

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

    } catch(Exception e){
        e.printStackTrace();
    }
}

How canI fix it?

Dani Garcia
  • 464
  • 1
  • 6
  • 18
  • if its android >= 6.0 apart from permission in manifest you need user permission during runtime otherwise it wont have access. You can manually enable it from settings -> applications -> application manager -> permission , this is a reason i can think... :) hope it helps – Ahmad Mar 24 '17 at 00:31
  • I already enabled the storage permission manually, but it still does not work – Dani Garcia Mar 24 '17 at 00:34
  • Try this File file = new File("/storage/emulated"); Log.e("Check","checking parent exists "+file.exists()); before any other line of code to confirm that parent directory exists ( just to confirm you can access the sdcard and that parent exits ) – Ahmad Mar 24 '17 at 00:46
  • I already try it, I got this message on logcat: E/Check: checking parent exists true. I think this means that parent directory exists and I have access. – Dani Garcia Mar 24 '17 at 00:56
  • maybe users are not allowed to create files in /storage/emulated ... depends on your android version / device manufacturer ... you could try to create a file there using some kind of file explorer to check this ... – PrfctByDsgn Mar 24 '17 at 08:21
  • can u rather use this Environment.getExternalStorage()+File.seprator+"FileTest1.zip" instead of storage/emulated and see if it works... also refer to http://stackoverflow.com/questions/9530921/list-all-the-files-from-all-the-folder-in-a-single-list – Ahmad Mar 24 '17 at 10:22
  • I finally resolved the issue, I think the problem was the file path. I was writting manually the path. I changed "/storage/emulated/" to getFilesDir().getPath() and it seems to work fine – Dani Garcia Mar 24 '17 at 22:58

0 Answers0