我想從一個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代碼。
請有人可以幫助我做到這一點?
由於
圖像託管在哪裏? flicker.com? – 2012-03-15 17:01:52
你可以把你的問題本身的XML字符串的示例輸出。這將有所幫助... – 2012-03-15 17:05:06