日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android代码(Handler的运用),HttpURLConnection的应用,将url图片地址转换成图片。

發布時間:2024/9/27 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android代码(Handler的运用),HttpURLConnection的应用,将url图片地址转换成图片。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


1 布局文件,

<LinearLayout 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:orientation="vertical"

??? tools:context=".MainActivity" >

?

??? <ImageView

??????? android:id="@+id/iv_icon"

??????? android:layout_width="fill_parent"

??????? android:layout_height="0dip"

??????? android:layout_weight="1" />

?

??? <LinearLayout

??????? android:layout_width="fill_parent"

??????? android:layout_height="wrap_content"

??????? android:orientation="horizontal" >

?

??????? <EditText

??????????? android:id="@+id/et_url"

??????????? android:layout_width="0dip"

??????????? android:text="http://img0.hao123.com/data/1_489a5f7dfbcbb624a231f372e4bdffce_310"

??????????? android:layout_height="wrap_content"

??????????? android:singleLine="true"

??????????? android:layout_weight="1" />

?

??????? <Button

??????????? android:id="@+id/btn_submit"

??????????? android:layout_width="wrap_content"

??????????? android:layout_height="wrap_content"

??????????? android:text="Go"

??????? ????android:textSize="20sp" />

??? </LinearLayout>

?

</LinearLayout>

Android代碼(Handler的運用),HttpURLConnection的應用,將url圖片地址轉換成圖片。

package com.itheim28.submitdata;

?

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

?

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.Toast;

?

public class MainActivity extends Activity implements OnClickListener {

?

??? private static final String TAG = "MainActivity";

??? protected static final int ERROR = 1;

??? private EditText etUrl;

??? private ImageView ivIcon;

??? private final int SUCCESS = 0;

???

??? private Handler handler = new Handler() {

?????? public void handleMessage(Message msg) {

?????????? super.handleMessage(msg);

??????????

?????????? Log.i(TAG, "what = " + msg.what);

?????????? if (msg.what == SUCCESS) {

????????????? ivIcon.setImageBitmap((Bitmap)msg.obj);

?????????? } else if (msg.what == ERROR) {

????????????? Toast.makeText(MainActivity.this, "抓起失敗", 0).show();

?????????? }

?????? }

??? };

???

??? @Override

??? protected void onCreate(Bundle savedInstanceState) {

?????? super.onCreate(savedInstanceState);

?????? setContentView(R.layout.activity_main);

??????

?????? ivIcon = (ImageView) findViewById(R.id.iv_icon);

?????? etUrl = (EditText) findViewById(R.id.et_url);

??????

?????? findViewById(R.id.btn_submit).setOnClickListener(this);

??? }

?

??? @Override

??? public void onClick(View v) {

?????? final String url = etUrl.getText().toString();

??????

?????? new Thread(new Runnable() {

?

?????????? @Override

?????????? public void run() {

????????????? Bitmap bitmap = getImageFromNet(url);

?

//??????????? ivIcon.setImageBitmap(bitmap);???? // 設置imageView顯示的圖片

????????????? if(bitmap != null) {

????????????????? Message msg = new Message();

????????????????? msg.what = SUCCESS;

????????????????? msg.obj = bitmap;

????????????????? handler.sendMessage(msg);

????????????? } else {

????????????????? Message msg = new Message();

????????????????? msg.what = ERROR;

????????????????? handler.sendMessage(msg);

????????????? }

?????????? }}).start();

??????

??? }

???

??? /**

??? ?* 根據url連接取網絡抓去圖片返回

??? ?* @param url

??? ?* @return url對應的圖片

??? ?*/

??? private Bitmap getImageFromNet(String url) {

?????? HttpURLConnection conn = null;

?????? try {

?????????? URL mURL = new URL(url); // 創建一個url對象

??????????

?????????? // 得到http的連接對象

?????????? conn = (HttpURLConnection) mURL.openConnection();

??????????

?????????? conn.setRequestMethod("GET");????? // 設置請求方法為Get

?????????? conn.setConnectTimeout(10000);???? // 設置連接服務器的超時時間, 如果超過10秒鐘, 沒有連接成功, 會拋異常

?????????? conn.setReadTimeout(5000);????? // 設置讀取數據時超時時間, 如果超過5, 拋異常

??????????

?????????? conn.connect();????? // 開始鏈接

??????????

?????????? int responseCode = conn.getResponseCode(); // 得到服務器的響應碼

?????????? if(responseCode == 200) {

????????????? // 訪問成功

????????????? InputStream is = conn.getInputStream();?? // 獲得服務器返回的流數據

?????????????

????????????? Bitmap bitmap = BitmapFactory.decodeStream(is); // 根據 流數據 創建一個bitmap位圖對象

?????????????

????????????? return bitmap;

?????????? } else {

????????????? Log.i(TAG, "訪問失敗: responseCode = " + responseCode);

?????????? }

?????? } catch (Exception e) {

?????????? e.printStackTrace();

?????? } finally {

?????????? if(conn != null) {

????????????? conn.disconnect();?????? // 斷開連接

?????????? }

?????? }

?????? return null;

??? }

}

?

總結

以上是生活随笔為你收集整理的Android代码(Handler的运用),HttpURLConnection的应用,将url图片地址转换成图片。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。