2011-06-14 60 views
2

我使用API​​(...)我收到一個包含png的url,並且我無法顯示圖片。我如何下載並在Android視圖中放置圖像

我必須下載圖片並顯示出來。我把一個手勢要求其他圖片,所以我不得不刪除舊圖片時,當用戶問最新的一個(當他在圖片上做手勢)

所以,我有一種方法,下載並顯示圖片(但屏幕仍黑色...):

private void downloadImage() { 
    Bitmap bitmap = null; 
    try { 
     URL urlImage = new URL("http://www.google.fr/intl/en_com/images/srpr/logo1w.png"); 
     HttpURLConnection connection = (HttpURLConnection) urlImage.openConnection(); 
     InputStream inputStream = connection.getInputStream(); 
     bitmap = BitmapFactory.decodeStream(inputStream); 
     Log.v("", "is showed"); 
     image.setImageBitmap(bitmap); 
     Log.v("", "isn't"); 
     } 
    catch (MalformedURLException e) { 
     e.printStackTrace(); 
     } 
    catch (IOException e) { 
     e.printStackTrace();} 
    } 
在onload

,我已經把

setContentView(R.layout.secondtab); 

圖像=(ImageView的)findViewById(R.id.ivImage);

我在@Override之前聲明瞭ImageView圖像。

在XML中,這就像:

<?xml version="1.0" encoding="utf-8"?> 

<android.gesture.GestureOverlayView 
    android:id="@+id/gestures" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:layout_weight="1.0" 
    android:eventsInterceptionEnabled="true" android:gestureStrokeType="single" 
    android:gestureColor="#00FFFFFF"/> 

<ImageView 
android:id="@+id/ivImage" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:scrollbars="vertical" 
/> 

感謝您的幫助:-)

+3

你有什麼試過,你卡在哪裏?從您的描述中很難判斷出您需要哪些幫助:建立與網址的連接?處理結果?一旦你有圖像顯示圖像?別的東西? – 2011-06-14 14:04:11

+0

@泰德:最初的帖子已更新..我想我必須有不同的想法,不是嗎? THAKS – clement 2011-06-14 14:42:18

回答

1

也許嘗試image.postInvalidate()?或者如果你在UI線程上運行所有這些,就讓它失效。

+0

你的錯誤是什麼?它是一個'OutOfMemoryException'或者你的代碼沒有顯示圖片? – hwrdprkns 2011-06-14 16:52:34

0

我的錯誤是,有相同的行兩次:

setContentView(R.layout.secondtab); 

現在它的運行,我只需要調整它的大小!

感謝StackOverFlow!

相關問題