2013-12-09 22 views
-4

我正在製作一個應用程序,並且我想在更改它們時更新圖像。我不確定如何顯示來自特定網址的圖片。如何在我的android應用程序中顯示來自某個URL的圖像?

任何幫助和建議表示讚賞

謝謝

+1

http://stackoverflow.com/questions/3375166/android-drawable-images-from-url?rq=1 – inankupeli

+0

我看見之間「一個巨大的差異保持圖像更新「和」顯示來自URL的圖像「。 –

回答

0

可能是重複的,如前面提到的,不過,我會使用的AsyncTask從一個URL加載圖像(這似乎並不在提供的鏈接中就是這種情況)。沿着這些線路

東西:

new AsyncTask<String, Void, Drawable>() { 
    @Override 
    protected Drawable doInBackground(String... params) { 
    Drawable result = null; 
    try { 
     URL url = new URL(params[0]); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     result = Drawable.createFromStream(input, "src"); 
    } catch (MalformedURLException muExp) { 
     Log.e(TAG, "Bad URL provided!", muExp); 
    } catch (IOException ioExp) { 
     Log.e(TAG, "Error loading content from URL!", ioExp); 
    } 
    return result; 
    } 

    @Override 
    protected void onPostExecute(Drawable result) { 
    // Do something with your Drawable, in UI, here... 
    } 
}.execute("http://myimageurl.com/image.jpg"); 
相關問題