2011-10-18 73 views
0

我需要從我的服務器下載圖像並將此圖像加載到imageview中。所以我有一個問題 - 我可以下載圖像到內存中並將其設置爲ImageView,而不保存在SD卡/本地存儲上?或者我必須下載到一些文件存儲?如果可能,請給我舉例。如何在Java,Android下載ImageView ImageView?

+0

可能重複[如何從URL中的應用程序下載圖像](http://stackoverflow.com/questions/ 7763841 /如何下載圖像從應用程序中的網址) –

回答

-1

例如代碼爲here。您可以使用BitmapFactory來實現自己的目標:

HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection(); 
conn.setDoInput(true); 
conn.connect(); 
InputStream is = conn.getInputStream(); 

bmImg = BitmapFactory.decodeStream(is); 
imView.setImageBitmap(bmImg); 
2

同樣的問題被問這樣你就可以搜索,

SAME Question

InputStream in = null; 
try 
{ 
    URL url = new URL("URL FOR IMAGE"); 
    URLConnection urlConn = url.openConnection(); 
    HttpURLConnection httpConn = (HttpURLConnection) urlConn; 
    httpConn.connect(); 
    in = httpConn.getInputStream(); 
} 
catch (MalformedURLException e) 
{ 
    e.printStackTrace(); 
} 
catch (IOException e) 
{ 
    e.printStackTrace(); 
} 
bmpimg = BitmapFactory.decodeStream(in); 
ImageView iv = "MY IMAGEVIEW"; 
iv.setImageBitmap(bmimg); 
+0

這應該是作爲一個評論(可能重複)。 –

+0

ohk..thankx..will刪除這個答案並發表評論..請繼續教我Android和StackOverflow ...你真的很有幫助。 – MKJParekh

+0

@PareshMayani如果您對此有所瞭解,請參閱此問題http://stackoverflow.com/questions/7806261/strange-behavior-of-android-videoview-cant-play-video/7806881#7806881 – MKJParekh

0

這可以幫助你。

Bitmap bmImg; 
void downloadFile(String fileUrl){ 
     URL myFileUrl =null;   
     try { 
      myFileUrl= new URL(fileUrl); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection(); 
      conn.setDoInput(true); 
      conn.connect(); 
      InputStream is = conn.getInputStream(); 

      bmImg = BitmapFactory.decodeStream(is); 
      imView.setImageBitmap(bmImg); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
} 

來源:http://en.androidwiki.com/wiki/Loading_images_from_a_remote_server

看到這個太

http://ballardhack.wordpress.com/2010/04/05/loading-remote-images-in-a-listview-on-android/