2012-11-08 47 views
0

下載文件如果我嘗試從網站下載圖像(使用http連接),我沒有問題,如果下載在我的android服務中執行,而如果我嘗試從同一個方法下載文件活動出現此錯誤:錯誤從活動

11-08 13:27:36.800: W/System.err(8010): android.os.NetworkOnMainThreadException 
11-08 13:27:36.800: W/System.err(8010):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077) 
11-08 13:27:36.800: W/System.err(8010):  at java.net.InetAddress.lookupHostByName(InetAddress.java:477) 
11-08 13:27:36.800: W/System.err(8010):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277) 
11-08 13:27:36.800: W/System.err(8010):  at java.net.InetAddress.getAllByName(InetAddress.java:249) 
11-08 13:27:36.800: W/System.err(8010):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69) 
11-08 13:27:36.800: W/System.err(8010):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48) 
11-08 13:27:36.800: W/System.err(8010):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304) 
11-08 13:27:36.800: W/System.err(8010):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89) 
11-08 13:27:36.800: W/System.err(8010):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292) 
11-08 13:27:36.800: W/System.err(8010):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274) 
11-08 13:27:36.800: W/System.err(8010):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038) 
11-08 13:27:36.800: W/System.err(8010):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHeaderField(HttpURLConnectionImpl.java:481) 
11-08 13:27:36.800: W/System.err(8010):  at java.net.URLConnection.getHeaderFieldInt(URLConnection.java:572) 
11-08 13:27:36.800: W/System.err(8010):  at java.net.URLConnection.getContentLength(URLConnection.java:335) 

我解決調用問題,從活動,這樣我的下載方法:

 new Thread(new Runnable() { 
     public void run() { 
      downloadFile(imagePath); 
       } 
       }).start(); 

回答

2

Cehck這

NetworkOnMainThreadException:應用程序嘗試在其主線程上執行聯網操作時引發的異常。

你應該在asynctask上調用sendfeedback方法,然後只有上面的代碼才能工作。由於Web服務器花費大量時間響應主線程變得無法響應。爲了避免它,你應該在另一個線程上調用它。因此,asynctask更好。

http://android-developers.blogspot.in/2009/05/painless-threading.html

+0

謝謝。我使用線程 – GVillani82

+0

解決了問題現在我只有一個問題:我希望我的主線程等待下載,然後繼續執行。我能做什麼? – GVillani82

0

試試這個代碼從網上下載圖像。

String myfeed="your url"; 

       URL url; 
       try { 
        url = new URL(myfeed); 
        InputStream content = (InputStream)url.getContent(); 
        d = Drawable.createFromStream(content , "src"); 

        Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); 

        String path = Images.Media.insertImage(getContentResolver(),bitmap,mImageName, null); 
     }catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

添加這在您的活動

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy);