xml文件代碼部分

<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" > <ImageView android:id="@+id/p_w_picpath" 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/edit" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" android:text="https://cache.yisu.com/upload/information/20200311/46/199580.jpg" /> <Button android:id="@+id/go" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go" android:textSize="20sp" /> </LinearLayout> </LinearLayout>
Activity
package com.example.android01;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private ImageView p_w_picpath;
private EditText edit;
private Button but;
private final int success=0;
private Handler handler=new Handler(){
@Override
public void handleMessage(android.os.Message msg) {
Bitmap b=(Bitmap)msg.obj;
if(b!=null)
{
if(msg.what==success)
{
p_w_picpath.setImageBitmap(b);
}
}else{
Toast.makeText(getApplicationContext(), "獲取圖片錯(cuò)誤", 0).show();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
p_w_picpath=(ImageView) findViewById(R.id.p_w_picpath);
edit=(EditText) findViewById(R.id.edit);
but=(Button) findViewById(R.id.go);
but.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final String url=edit.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
Bitmap bit=getImageFromNet(url);
// p_w_picpath.setImageBitmap(bit);
Message msg=new Message();
msg.obj=bit;
msg.what=success;
handler.sendMessage(msg);
}
}).start();
}
private Bitmap getImageFromNet(String url){
HttpURLConnection conn=null;
try {
URL mURL=new URL(url);//創(chuàng)建一個(gè)URL連接
conn=(HttpURLConnection) mURL.openConnection();//得到一個(gè)connection對(duì)象
conn.setRequestMethod("GET");//設(shè)置請(qǐng)求方法為GET
conn.setConnectTimeout(10000);//設(shè)置連接超時(shí)時(shí)間
conn.setReadTimeout(5000);//設(shè)置讀取過(guò)程中的異常
conn.connect();
int responseCode=conn.getResponseCode();//獲取響應(yīng)碼,404,500,200
if(responseCode==200){
//訪(fǎng)問(wèn)成功
InputStream in=conn.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(in);//將從服務(wù)器獲取的流變成Bitmap位圖
return bitmap;
}else{
Toast.makeText(this, "獲取圖片失敗", 0).show();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(conn!=null)
{
conn.disconnect();
}
}
return null;
}
}另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前題目:Android中子線(xiàn)程網(wǎng)絡(luò)查看器與Handler消息處理器-創(chuàng)新互聯(lián)
分享鏈接:http://chinadenli.net/article34/ccghpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶(hù)體驗(yàn)、品牌網(wǎng)站設(shè)計(jì)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、企業(yè)建站、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容