Android實現(xiàn)網(wǎng)絡(luò)圖片瀏覽器
本文實例為大家分享了Android網(wǎng)絡(luò)圖片瀏覽器的制作過程,供大家參考,具體內(nèi)容如下
一、創(chuàng)建一個“網(wǎng)絡(luò)圖片瀏覽器的應(yīng)用程序”,并設(shè)計用戶交互界面,“網(wǎng)絡(luò)圖片瀏覽器”對應(yīng)的布局文件(activity_main.xml)代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.bz0209.myapplication.MainActivity"> <ImageView android:layout_weight="1000" android:id="@+id/iv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <EditText android:singleLine="true" android:id="@+id/et_path" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://b.hiphotos.baidu.com/image/w%3D310/sign=a439f5b24510b912bfc1f0fff3fdfcb5/83025aafa40f4bfb92c52c5d014f78f0f73618a5.jpg" android:hint="請輸入圖片路徑" /> <Button android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="瀏覽" /> </LinearLayout>
效果圖如下:

二、編寫界面交互代碼(MainActivity),當(dāng)界面創(chuàng)建好后,需要在MainActivity里面編寫與界面交互的代碼。用于實現(xiàn)請求指定地址的網(wǎng)絡(luò)圖片,并將服務(wù)器返回的圖片展現(xiàn)在界面上。具體代碼如下:
package com.example.bz0209.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
protected static final int CHANGE_UI=1;
protected static final int ERROR=2;
private EditText et_path;
private ImageView iv;
//主線創(chuàng)建消息處理器
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg){
if (msg.what==CHANGE_UI){
Bitmap bitmap=(Bitmap)msg.obj;
iv.setImageBitmap(bitmap);
}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);
et_path=(EditText)findViewById(R.id.et_path);
iv=(ImageView) findViewById(R.id.iv);
}
public void click(View view){
final String path=et_path.getText().toString().trim();
if (TextUtils.isEmpty(path)){
Toast.makeText(this,"圖片路徑不能為空",0).show();
}
else {
//子線程請求網(wǎng)絡(luò),Android 4.0以后訪問網(wǎng)絡(luò)不能放在子線程中
new Thread(){
private HttpURLConnection conn;
private Bitmap bitmap;
public void run(){
//鏈接服務(wù)器get請求,獲取圖片
try{
//創(chuàng)建URL對象
URL url =new URL(path);
conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatib;MSIE 6.0;Window NT 5.1;"+"SV1;" +
".NET4.0C;.NET4.0E;NET CLK 2.0.50727;"+".NET CLR 3.0..4506.2152;.NET CLR 3.5.30729; Shuame )");
int code=conn.getResponseCode();
if(code==200){
InputStream is=conn.getInputStream();
bitmap= BitmapFactory.decodeStream(is);
Message msg=new Message();
msg.what=CHANGE_UI;
msg.obj=bitmap;
handler.sendMessage(msg);
}
else {
Message msg=new Message();
msg.what=ERROR;
handler.sendMessage(msg);
}
}catch (Exception e){
e.printStackTrace();
Message msg=new Message();
msg.what=ERROR;
}
}
}.start();
}
}
}
三、添加權(quán)限:由于網(wǎng)絡(luò)圖片瀏覽器需要請求網(wǎng)絡(luò),因此需要在清單文件中配置相應(yīng)的權(quán)限,具體操作如下圖所示:

四、運行瀏覽圖片 ,結(jié)果圖如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實現(xiàn)TextView中的部分文字實現(xiàn)點擊跳轉(zhuǎn)功能
在移動端應(yīng)用中,往往需要在一段文字中讓某幾個關(guān)鍵詞或短語具備點擊跳轉(zhuǎn)功能,如果將整段文字放到 Button、Link 或單獨的 View 中,通常會帶來布局復(fù)雜、樣式難以統(tǒng)一、可維護性差等問題,所以本文將給大家介紹Android實現(xiàn)TextView中的部分文字實現(xiàn)點擊跳轉(zhuǎn)功能2025-04-04
使用RecyclerView添加Header和Footer的方法
RecyclerView雖然作為ListView的替代者有著較好的性能提升,但是ListView的一些常用功能卻沒有提供,比如我們平時會經(jīng)常用到的addHeaderView,addFooterView,既然RecyclerView沒有提供這個方法,我們應(yīng)該如何為列表添加頭部和底部呢,接下來通過本文給大家介紹2016-03-03
Android 2.3.7.r1 camera錄像過程中按menu菜單鍵時會停止錄像
android GB版本的camera錄像過程中按“菜單”鍵會停止錄像,改成錄像時按menu鍵不做處理,具體修改方法如下,感興趣的朋友可以參考下哈2013-06-06
Android編程開發(fā)之TextView文字顯示和修改方法(附TextView屬性介紹)
這篇文章主要介紹了Android編程開發(fā)之TextView文字顯示和修改方法,結(jié)合實例詳細(xì)分析了Android中TextView控件關(guān)于文字的顯示及修改技巧,并附帶了TextView屬性介紹,需要的朋友可以參考下2015-12-12

