2011-03-09 130 views
0

我在网络上发现此源代码并对其进行了一些修改。但是我收到一个错误:java.io.FileNotFoundException /data/datafile.zip。 我该怎么做才能让它运行?我必须先创建文件吗?当我尝试下载文件时出现错误

感谢,矽格

private Thread checkUpdate = new Thread() { 
    public void run() { 
     try { 
      long startTime = System.currentTimeMillis(); 
      Log.d("Zip Download", "Start download"); 
      File file = new File(Environment.getDataDirectory(), "datafil.zip"); 
      Log.d("Zip Download", file.getAbsolutePath()); 

      URL updateURL = new URL("http://dummy.no/bilder/bilder/XML_Item_Expo_01.zip"); 
      URLConnection conn = updateURL.openConnection(); 
      InputStream is = conn.getInputStream(); 
      BufferedInputStream bis = new BufferedInputStream(is); 
      ByteArrayBuffer baf = new ByteArrayBuffer(50); 

      int current = 0; 
      while((current = bis.read()) != -1){ 
       baf.append((byte)current); 
      } 

      /* Convert the Bytes read to a String. */ 
      FileOutputStream fos = new FileOutputStream(file); 
      fos.write(baf.toByteArray()); 
      fos.close(); 
      Log.d("Zip Download", "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + " sec"); 
     } catch (Exception e) { 
      Log.d("Zip Download", "Error: " + e); 
     } 
    } 
}; 

回答

1

Environment.getDataDirectory()不回,你可以将文件的路径。您应该使用以下其中一种方法:

  • Environment.getExternalStorageDirectory()为您提供了外部存储(SD卡)的路径。
  • getFilesDir()来自活动或其他上下文。给予应用程序的内部文件存储

路径您也可以拨打openFileOutput()一个字符串的文件名(没有路径,只是文件),它会在一个镜头供您使用打开FileOutputStream中,并创建该文件的所有。

希望有助于!

相关问题