2012-03-15 80 views
1

我想從一個URL獲得一個遠程JPG文件,並將其放入一個位圖。我正在爲Android開發,我的手機上有互聯網,清單上有互聯網許可。如何使用USER和密碼從URL獲取遠程JPG?

問題是URL需要用戶和密碼。

我知道如何讓遠程圖像轉換成位圖無密碼:

public static Bitmap getBitmapFromURL(String src) { 
    try { 
     URL url = new URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } catch (IOException e) {e.printStackTrace();} 
    return null; 
} 

,我知道如何從URL獲得一個XML字符串,用戶名和密碼:

public String getXML(String url){ 
    StringBuilder builder = new StringBuilder(); 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpResponse response; 
    try { 
     ((AbstractHttpClient) httpclient).getCredentialsProvider().setCredentials(
       new org.apache.http.auth.AuthScope(null,-1), 
       new org.apache.http.auth.UsernamePasswordCredentials(MagazineStatus._username, MagazineStatus._password)); 

     response = httpclient.execute(new HttpGet(url)); 
     StatusLine statusLine = response.getStatusLine(); 
     if(statusLine.getStatusCode() == HttpStatus.SC_OK) { 
      try {     
       HttpEntity entity = response.getEntity(); 
       InputStream content = entity.getContent(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
       String line; 
       while ((line = reader.readLine()) != null) builder.append(line);   
      }catch(Exception ex) {Log.e("DBF Error",ex.toString());}     
     }else { 
      response.getEntity().getContent().close(); 
      throw new IOException(statusLine.getReasonPhrase()); 
     } 
    }catch(ClientProtocolException cpe) {Log.e("ClientProtocolException @ at FPT",cpe.toString());} catch(Exception ex) {Log.e("Exception at FETCHPROJECTASK",ex.toString());} 
    return builder.toString(); 
} 

所以,我需要從URL下載一個位圖,但使用USER和PASSWORD,就像在XML方法中一樣。但我不知道如何合併sorce代碼。

請有人可以幫助我做到這一點?

由於

+0

圖像託管在哪裏? flicker.com? – 2012-03-15 17:01:52

+0

你可以把你的問題本身的XML字符串的示例輸出。這將有所幫助... – 2012-03-15 17:05:06

回答

1

這並不真正相關,但這裏有一個建議。您可能想要使用PNG文件而不是JPEG文件,因爲PNG在數據壓縮時不會丟失質量(Android也偏愛使用PNG)。這樣,您擁有的視圖在所有設備上看起來都差不多。

0

我有類似的問題,和解決它,我插在http請求下面的頭: 部首:授權 值:「基本‘+ Base64Coder.encodeString(用戶+’:」 +通過)

Base64Coder是一個免費的類,幫助我將用戶+「:」+傳遞字符串轉換爲Base64格式。你可以從互聯網上的幾個地方下載代碼。

關於如何在http請求中插入一個新的頭文件,我使用了HttpPost和HttpGet對象,而不是HttpURLConnection。我不知道是否可以用HttpURLConnection完成。要設置標題,您只需使用setHeader方法。

我包括一個鏈接,顯示如何使用httpget。 http://w3mentor.com/learn/java/android-development/android-http-services/example-of-http-get-request-using-httpclient-in-android/

0

你不能通過使用相同的任務來做到這一點。您需要創建另一個asyntask,它會爲您提取圖像並返回位圖。我也是這麼做的。使用此代碼..

Bitmap image = null; 

    @Override 
    protected Bitmap doInBackground(String... params) { 
     try { 
// param[0] would be replaced with your image path. 
      image = BitmapFactory.decodeStream((InputStream)new URL(params[0]).getContent()); 
     } catch (MalformedURLException e) { 
      Log.e("MalformedURLException",e.toString()); 
     } catch (IOException e) { 
      Log.e("IOException",e.toString()); 
     }  
     return image;  
    } 

希望,它會再次爲你工作。