Android AsyncTask downloads files as directories

Go To


I'm trying to download a file, but its downloading it as a directory, and Im not sure what the problem is. The class Im using ive used in other projects without issue.

outfile is the full path to the output directory, /mnt/sdcard/file.ext

public class DownloadFile {
    String outfile;
    String zipfile;
    Context c;
    Notification notification;
    NotificationManager notificationManager;
    ProgressBar progressBar;
    private int progress = 10;
    public DownloadFile(String url, String out, String zip, Context ctx) {        
        c = ctx;
        zipfile = zip;
        outfile = out;

        notificationManager = (NotificationManager) c.getSystemService(

        new Download().execute(url);

    private class Download extends AsyncTask<String, Integer, String>{
        protected String doInBackground(String... urls) {
            int count;
            try {
                URL url = new URL(urls[0]);
                URLConnection conexion = url.openConnection();
                int lenghtOfFile = conexion.getContentLength();

                InputStream input = new BufferedInputStream(url.openStream());
                //LINE 55 is below
                OutputStream output = new FileOutputStream(outfile);

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = != -1) {
                    total += count;
                    output.write(data, 0, count);

            } catch (Exception e) {
            return zipfile;

        protected void onPreExecute() {                     
            Intent intent = new Intent(c, DownloadFile.class);
            final PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, intent, 0);

            notification = new Notification(R.drawable.ic_launcher, "simulating a download", System
            notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
            notification.contentView = new RemoteViews(c.getPackageName(), R.layout.download_progress);
            notification.contentIntent = pendingIntent;
            notification.contentView.setImageViewResource(, R.drawable.ic_launcher);
            notification.contentView.setTextViewText(, "simulation in progress");
            notification.contentView.setProgressBar(, 100, progress, false);

            notificationManager.notify(42, notification);

        protected void onProgressUpdate(Integer... args){
            notification.contentView.setProgressBar(, 100, args[0], false);         

        protected void onPostExecute(String f) {
            Toast.makeText(c, "DONE WITH "+f, 9965454).show();

This is the error that it throws.

W/System.err(15992): /mnt/sdcard/t3hh4xx0r/romCrawler/ open failed: EISDIR (Is a directory)
W/System.err(15992):    at
W/System.err(15992):    at<init>(
W/System.err(15992):    at<init>(
W/System.err(15992):    at<init>(
W/System.err(15992):    at com.t3hh4xx0r.romcrawler.DownloadFile$Download.doInBackground(
W/System.err(15992):    at com.t3hh4xx0r.romcrawler.DownloadFile$Download.doInBackground(
W/System.err(15992):    at android.os.AsyncTask$
W/System.err(15992):    at java.util.concurrent.FutureTask$Sync.innerRun(
W/System.err(15992):    at
W/System.err(15992):    at java.util.concurrent.ThreadPoolExecutor.runWorker(
W/System.err(15992):    at java.util.concurrent.ThreadPoolExecutor$
W/System.err(15992):    at
W/System.err(15992): Caused by: open failed: EISDIR (Is a directory)
W/System.err(15992):    at Method)
W/System.err(15992):    at
W/System.err(15992):    at
W/System.err(15992):    ... 11 more

Why is it trying to download the file as a directory?

EDIT For a little more info on the problem, the path listed in the error, /mnt/sdcard/t3hh4xx0r/romCrawler/ is the path and file name of the file Im trying to download. Instead of downloading though, it creates a folder with that name. I have a folder on my sdcard named at `/mnt/sdcard/t3hh4xx0r/romCrawler/

2012-04-04 01:12
by r2DoesInc
Added a little more information - r2DoesInc 2012-04-04 02:30


You have to make sure, that there is no directory with the same name. I had a similar problem. In my case it was created by File.mkdirs().

I would try to remove the directory before creating the FileOutputStream:

new File(outfile).delete();
2012-04-05 16:57
by Floern


Do all the folders leading up to the location where you're trying to download the file to exist? FileOutputStream does not create folders, only files, and then only if the path leading up to the file exists.

2012-04-04 01:32
by Tommy B
yes. the directory is there - r2DoesInc 2012-04-04 01:39
Which folder specifically is that? I ask because I'm seeing two different ones. One is the one you specified in the question, the other (quite different to the first one) is listed in the exception - Tommy B 2012-04-04 01:59
the one i have in the questioin is just an example. the one in the error is the actual file name. instead of the file being downloaded though, it is creating a folder with the .zip file name - r2DoesInc 2012-04-04 02:05