


下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
【移動應用開發(fā)技術】Android中怎么顯示網絡圖片
本篇文章給大家分享的是有關Android中怎么顯示網絡圖片,在下覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著在下一起來看看吧。Android顯示網絡圖片Step1:1、創(chuàng)建你的Activity,本例中以ViewWebImageActivity說明;2、ViewWebImageActivity中的代碼如下:String
imageUrl
=
"/baidu/pic/item/7d8aebfebf3f9e125c6008d8.jpg";
//這就是你需要顯示的網絡圖片網上隨便找的
Bitmap
bmImg;
ImageView
imView;
Button
button1;
@Override
public
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imView
=
(ImageView)
findViewById(R.id.imview);
imView.setImageBitmap(returnBitMap(imageUrl));
}
public
Bitmap
returnBitMap(String
url)
{
URL
myFileUrl
=
null;
Bitmap
bitmap
=
null;
try
{
myFileUrl
=
new
URL(url);
}
catch
(MalformedURLException
e)
{
e.printStackTrace();
}
try
{
HttpURLConnection
conn
=
(HttpURLConnection)
myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream
is
=
conn.getInputStream();
bitmap
=
BitmapFactory.decodeStream(is);
is.close();
}
catch
(IOException
e)
{
e.printStackTrace();
}
return
bitmap;
}String
imageUrl
=
"/baidu/pic/item/7d8aebfebf3f9e125c6008d8.jpg";
//這就是你需要顯示的網絡圖片網上隨便找的
Bitmap
bmImg;
ImageView
imView;
Button
button1;
@Override
public
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imView
=
(ImageView)
findViewById(R.id.imview);
imView.setImageBitmap(returnBitMap(imageUrl));
}
public
Bitmap
returnBitMap(String
url)
{
URL
myFileUrl
=
null;
Bitmap
bitmap
=
null;
try
{
myFileUrl
=
new
URL(url);
}
catch
(MalformedURLException
e)
{
e.printStackTrace();
}
try
{
HttpURLConnection
conn
=
(HttpURLConnection)
myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream
is
=
conn.getInputStream();
bitmap
=
BitmapFactory.decodeStream(is);
is.close();
}
catch
(IOException
e)
{
e.printStackTrace();
}
return
bitmap;
}3、其中,returnBitMap(String
url)
方法就是具體實現(xiàn)網絡圖片轉換成bitmap。Android顯示網絡圖片Step2:1、修改你的main.xml文件如下:<
?xml
version="1.0"
encoding="utf-8"?>
<
LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<
ImageView
android:id="@+id/imview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<
/LinearLayout>
<
?xml
version="1.0"
encoding="utf-8"?>
<
LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<
ImageView
android:id="@+id/imview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<
/LinearLayout>
Android顯示網絡圖片Step3
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 農牧設備回收合同范本
- app軟件采購合同范本
- 勞動合同范本 簡約
- 佛山機械購銷合同范本
- 京東供貨方合同范本
- 加工協(xié)作合同范本
- 勞務合同范本保密協(xié)議
- 動漫公司產品合同范本
- 修理提成合同范例
- 全款買車正規(guī)合同范本
- 經典文學作品中的女性形象研究外文文獻翻譯2016年
- 控股集團公司組織架構圖.docx
- 高爐煤氣安全知識的培訓
- 2008 年全國高校俄語專業(yè)四級水平測試試卷
- 需求供給與均衡價格PPT課件
- 最常用2000個英語單詞_(全部標有注釋)字母排序
- 人造革的幾種生產制造方法
- 在銀行大零售業(yè)務工作會議上的講話講解學習
- 古代傳說中的藝術形象-
- 水電站大壩土建安裝工程懸臂模板施工手冊
- 三體系內審檢查表(共58頁).doc
評論
0/150
提交評論