2012-04-04 60 views
1

我试图下载一个文件,但将其作为目录下载,并且我不确定问题出在哪。 Im使用其他项目中使用的类im没有问题。Android AsyncTask将文件下载为目录

OUTFILE是输出目录的完整路径,/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(
       c.NOTIFICATION_SERVICE); 

     new Download().execute(url); 
    } 

    private class Download extends AsyncTask<String, Integer, String>{ 
     @Override 
     protected String doInBackground(String... urls) { 
      int count; 
      try { 
       URL url = new URL(urls[0]); 
       URLConnection conexion = url.openConnection(); 
       conexion.connect(); 
       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 = 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) { 
       e.printStackTrace(); 
      } 
      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 
        .currentTimeMillis()); 
      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.id.status_icon, R.drawable.ic_launcher); 
      notification.contentView.setTextViewText(R.id.status_text, "simulation in progress"); 
      notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false); 

      notificationManager.notify(42, notification); 
     }  

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

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

这是它抛出的错误。

W/System.err(15992): java.io.FileNotFoundException: /mnt/sdcard/t3hh4xx0r/romCrawler/imoseyon_leanKernel_v1.9.0gnexus.zip: open failed: EISDIR (Is a directory) 
W/System.err(15992): at libcore.io.IoBridge.open(IoBridge.java:406) 
W/System.err(15992): at java.io.FileOutputStream.<init>(FileOutputStream.java:88) 
W/System.err(15992): at java.io.FileOutputStream.<init>(FileOutputStream.java:128) 
W/System.err(15992): at java.io.FileOutputStream.<init>(FileOutputStream.java:117) 
W/System.err(15992): at com.t3hh4xx0r.romcrawler.DownloadFile$Download.doInBackground(DownloadFile.java:55) 
W/System.err(15992): at com.t3hh4xx0r.romcrawler.DownloadFile$Download.doInBackground(DownloadFile.java:1) 
W/System.err(15992): at android.os.AsyncTask$2.call(AsyncTask.java:264) 
W/System.err(15992): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
W/System.err(15992): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
W/System.err(15992): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
W/System.err(15992): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
W/System.err(15992): at java.lang.Thread.run(Thread.java:856) 
W/System.err(15992): Caused by: libcore.io.ErrnoException: open failed: EISDIR (Is a directory) 
W/System.err(15992): at libcore.io.Posix.open(Native Method) 
W/System.err(15992): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
W/System.err(15992): at libcore.io.IoBridge.open(IoBridge.java:390) 
W/System.err(15992): ... 11 more 

为什么试图下载该文件的目录?

编辑 有关问题多一点信息,在错误中列出的路径,/mnt/sdcard/t3hh4xx0r/romCrawler/imoseyon_leanKernel_v1.9.0gnexus.zip是文件我试着去下载的路径和文件名。它不是下载,而是创建一个具有该名称的文件夹。我在我的SD卡上有一个名为imoseyon_leanKernel_v1.9.0gnexus.zip的文件夹,位于`/mnt/sdcard/t3hh4xx0r/romCrawler/

+0

增添了些许的更多信息。 – r2DoesInc 2012-04-04 02:30:03

回答

6

您必须确保没有相同名称的目录。我有类似的问题。在我的情况下,它是由File.mkdirs()创建的。

我会尝试创建FileOutputStream之前删除的目录:

new File(outfile).delete(); 
1

是否所有文件夹导致您尝试下载文件的位置存在? FileOutputStream不会创建文件夹,只会创建文件,然后才会创建文件的路径。

+0

是的。该目录在那里。 – r2DoesInc 2012-04-04 01:39:54

+0

具体是哪个文件夹?我问,因为我看到两个不同的。一个是你在问题中指定的那个,另一个(与第一个完全不同)列在例外中。 – 2012-04-04 01:59:27

+0

我在问题中的一个只是一个例子。错误中的一个是实际的文件名。而不是正在下载的文件,它正在创建一个带有.zip文件名的文件夹。 – r2DoesInc 2012-04-04 02:05:57