您好,登錄后才能下訂單哦!
利用Http從指定地址下載圖片并顯示出來
/** * 簡單的封裝一個http下載類 * */ public class HttpUtils { //指定一張圖片 private String URL_PATH = "http://img.wangxiao.cn/files/RemoteFiles/20140420/095185001.jpg"; public HttpUtils(){ } //返回一個byte類型的數組 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; } }
//用于調用的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中別忘了 網絡權限 <uses-permission android:name="android.permission.INTERNET" />
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。