您好,登錄后才能下訂單哦!
利用Http從指定地址下載圖片并顯示出來
/** * 簡單的封裝一個http下載類 * */ public class HttpUtils { //指定一張圖片 private String URL_PATH = "http://img.wangxiao.cn/files/RemoteFiles/20140420/095185001.jpg"; public HttpUtils(){ } //返回一個byte類型的數(shù)組 public byte[] getData(){ HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(URL_PATH); HttpResponse httpResponse = null; try { httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200) { byte[] data = EntityUtils.toByteArray(httpResponse .getEntity()); return data; } } catch (Exception e) { e.printStackTrace(); } return null; } }
//用于調(diào)用的avtivity public class MainActivity extends Activity { private Button btnDown; private ImageView img; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnDown = (Button) findViewById(R.id.button1); img = (ImageView) findViewById(R.id.p_w_picpathView1); dialog = new ProgressDialog(this); dialog.setTitle("提示信息"); dialog.setMessage("正在下載中,請稍后..."); dialog.setCancelable(false); btnDown.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(mRunnable).start(); dialog.show(); } }); } private Runnable mRunnable = new Runnable() { @Override public void run() { // TODO Auto-generated method stub byte[] data = new HttpUtils().getData(); Message msg = Message.obtain(); msg.obj = data; msg.what = 0x001; handler.sendMessage(msg); } }; private Handler handler = new Handler() { // 在Handler中獲取消息,重寫handleMessage()方法 @Override public void handleMessage(Message msg) { // 判斷消息碼是否為0x001 if(msg.what == 0x001){ byte[] data=(byte[])msg.obj; Bitmap bmp=BitmapFactory.decodeByteArray(data, 0, data.length); img.setImageBitmap(bmp); dialog.dismiss(); } } }; }
//布局 .xml 很簡單只有兩個控件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="54dp" android:text="下載" /> <ImageView android:id="@+id/p_w_picpathView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="79dp" android:src="@drawable/ic_launcher" /> </RelativeLayout>
//最后manifest中別忘了 網(wǎng)絡權(quán)限 <uses-permission android:name="android.permission.INTERNET" />
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。