2011-07-07 16 views
0

我在嘗試從遠程URL創建位圖時遇到了一些問題。以下是摘錄:即使使用INTERNET權限,也無法從連接獲取圖像

Bitmap bm = null;  

    URL aURL = null; 
    try { 
     aURL = new URL("http://developer.android.com/assets/images/home/honeycomb-android.png");     
    } catch (MalformedURLException e) { 
     System.err.println(e); 
    } 

    if(aURL != null){ 
     URLConnection conn = null; 
     BufferedInputStream bis = null;    
     InputStream is = null; 

     try { 
      conn = aURL.openConnection(); 
      conn.connect(); 
      is = conn.getInputStream(); 
      bis = new BufferedInputStream(is); 
      bm = BitmapFactory.decodeStream(bis);     
     } catch (Exception e) { 
      System.err.println(e); 
     } finally { 
      if(bis != null){ 
       try { 
        bis.close(); 
       } catch (IOException e) {} 
      } 
      if(is != null){ 
       try { 
        is.close(); 
       } catch (IOException e) {} 
      } 
      conn = null; 
     }  
    } 

我在conn.connect()行中收到異常。它說 java.net.SocketException: Permission denied

我已經加入清單中的權限:

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      android:versionCode="1" 
      android:versionName="1.0" package="mypackage.namehere"> 
     <uses-sdk android:minSdkVersion="8" /> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
     <uses-permission android:name="android.permission.INTERNET"></uses-permission> 


     <application android:icon="@drawable/icon" android:label="@string/app_name"> 

     </application> 
    </manifest> 

我既可以從我的電腦瀏覽器和模擬器瀏覽器加載圖像。沒有防火牆阻止我的互聯網連接。 嘗試了幾個網址,但沒有任何工作。

我是否正確打開連接?我應該使用HttpConnection嗎?

+0

您是否對使用webview的使用感到滿意? –

+0

黑暗中射擊:你可以張貼你的清單嗎? – Otra

+0

@你在這裏,在上面編輯。 –

回答

0

試試這個。

Bitmap bm = null;  

URL aURL = null; 
try { 
    aURL = new URL("http://developer.android.com/assets/images/home/honeycomb-android.png");     
} catch (MalformedURLException e) { 
    System.err.println(e); 
} 

if(aURL != null){ 
    HttpURLConnection conn = null; 
    BufferedInputStream bis = null;    
    InputStream is = null; 

    try { 
     conn = (HttpURLConnection) aURL.openConnection(); 
     is = conn.getInputStream(); 
     bis = new BufferedInputStream(is); 
     bm = BitmapFactory.decodeStream(bis);     
    } catch (Exception e) { 
     System.err.println(e); 
    } finally { 
     if(bis != null){ 
      try { 
       bis.close(); 
      } catch (IOException e) {} 
     } 
     if(is != null){ 
      try { 
       is.close(); 
      } catch (IOException e) {} 
     } 
     conn = null; 
    }  
} 
+0

不起作用,拋出相同的異常。 –

+0

這很奇怪。我已經測試了我的編輯代碼和您的原始代碼,沒有任何問題。我唯一能想到的是,無論你是在代理還是防火牆後面,但你已經說過你沒有。 – Otra

0

好友容易,要添加的權限,在錯誤的地方

... 
    </application> 
    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
</manifest> 

你關閉你可以看到後?

+0

我沒有看到你的觀點。在我的AndroidManifest中,它們處於同一級別。我不認爲在元素之前或之後添加有任何區別。無論如何,它們都是使用插件清單編輯器添加的,所以我認爲它應該沒問題。 –

相關問題