2011-11-04 98 views
0

我正在使用Windows vista和Eclipse進行開發。我編寫簡單的 代碼來下載文件並將其存儲在我的SD卡上。但我得到 異常(文件未找到異常)。 這裏是我的代碼Android:無法從模擬器寫入SD卡

public void downloadNewapk() { 
    try { 
     URL url = new URL(apkURL.toString()); 
     HttpsURLConnection c = (HttpsURLConnection) url.openConnection(); 
     c.setRequestMethod("GET"); 
     c.setDoOutput(true); 
     c.connect(); 

     String PATH = Environment.getExternalStorageDirectory() 
       + "/download/"; 
     Log.v("log_tag", "PATH: " + PATH); 
     File file = new File(PATH); 
     if (!file.exists()) { 
      file.mkdirs(); 
     } 
     File outputFile = new File(file, fileName); 
     FileOutputStream fos = new FileOutputStream(outputFile); 

     InputStream is = c.getInputStream(); 

     byte[] buffer = new byte[1024]; 
     int len1 = 0; 
     while ((len1 = is.read(buffer)) != -1) { 
      fos.write(buffer, 0, len1); 
     } 
     fos.close(); 
     is.close(); 

    } catch (IOException e) { 
     Log.d("log_tag", "Error: " + e); 
    } 
    Log.v("log_tag", "Check: "); 
} 

我已經加入用戶權限在manifest.xml 和我 Environment.getExternalStorageState()檢查SD卡狀態它給人的「刪除」 但是,創造了當我加入1GB大小爲我的SD卡AVD。 我是新來的android和解決這個對我來說很重要 請任何人都可以幫我

+1

我建議你刪除模擬器並重新創建一個。一旦它爲我這樣工作。 –

+0

我前幾天也遇到同樣的問題,但我還沒有解決它。如果你解決了它,請讓我知道。:-) –

回答