Android 網(wǎng)絡(luò)圖片查看器與網(wǎng)頁(yè)源碼查看器
在AndroidManifest.xml里面先添加權(quán)限訪問網(wǎng)絡(luò)的權(quán)限:
<uses-permission android:name="android.permission.INTERNET"/>
效果圖如下:

下面是主要代碼:
package com.hb.neting;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView iv_show;
private EditText et_input;
private String path;
private int code;
private HttpURLConnection conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_show=(ImageView) findViewById(R.id.iv_show);
et_input=(EditText) findViewById(R.id.et_inpput);
}
@SuppressLint("ShowToast") public void chakan(View view){
path = et_input.getText().toString().trim();
if (TextUtils.isEmpty(path)) {
Toast.makeText(MainActivity.this, "不能輸入空的", 0).show();
return;
}
new Thread(){
public void run() {
try {
URL url = new URL(path);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
code = conn.getResponseCode();
if(code==200){
InputStream in = conn.getInputStream();
//解析圖片
final Bitmap stream = BitmapFactory.decodeStream(in);
runOnUiThread(new Runnable() {
public void run() {
//更新UI
iv_show.setImageBitmap(stream);
}
});
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
}
這是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" > <EditText android:id="@+id/et_inpput" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請(qǐng)輸入獲取圖片的地址:" /> <Button android:id="@+id/bt_read" android:onClick="chakan" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查看" /> <ImageView android:id="@+id/iv_show" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
源碼: http://pan.baidu.com/s/1bp6EwyF
接著看一下網(wǎng)頁(yè)源碼查看器的小案例:
既然都涉及到網(wǎng)絡(luò)的添加一個(gè)如上的網(wǎng)絡(luò)權(quán)限是必不可少的了,具體操做如上所示,先看效果圖:

主要代碼:
package com.hb.network;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.hb.utils.ReadStreamUtils;
public class MainActivity extends Activity {
protected static final int SUCESS = 0;
protected static final int EORR = 1;
private TextView tv_show;
private EditText et_input;
private URL url;
private String path;
@SuppressLint("HandlerLeak")
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SUCESS:
String content=(String) msg.obj;
tv_show.setText(content);
break;
case EORR:
Toast.makeText(MainActivity.this,"查看源碼失敗" , 0).show();
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_show=(TextView) findViewById(R.id.tv_show);
et_input=(EditText) findViewById(R.id.et_input);
}
public void onclick(View view){
path = et_input.getText().toString().trim();
if(TextUtils.isEmpty(path)){
return;
}new Thread(){
public void run() {
try {
url = new URL(path);
//判斷從EditText獲取的數(shù)據(jù)否為空
if(TextUtils.isEmpty(path)){
return;
}
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if(code == 200){
InputStream is= conn.getInputStream();
String content = ReadStreamUtils.Read(is);
Message msg = new Message();
msg.what=SUCESS;
msg.obj=content;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what=EORR;
handler.sendMessage(msg);
}
};
}.start();
}
}
package com.hb.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class ReadStreamUtils {
/**
* 讀取流的輸入
* @param is
* @return
* @throws IOException
*/
public static String Read(InputStream is) throws IOException{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
byte [] buffer=new byte[1024];
while((len=is.read(buffer))!=-1){
bos.write(buffer,0,len);
}
is.close();
bos.close();
String temp = bos.toString();
if(temp.contains("charset=utf-8")){
return bos.toString("utf-8");
}else if(temp.contains("charset=iso-8859-1")){
return bos.toString("iso-8859-1");
}
return null;
}
}
及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"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入要查看源碼的網(wǎng)址:" />
<Button
android:onClick="onclick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看"
android:textSize="25sp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
</LinearLayout>
源碼: http://pan.baidu.com/s/1bp6EwyF
http://pan.baidu.com/s/1c2H1JlI
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- android查看網(wǎng)絡(luò)圖片的實(shí)現(xiàn)方法
- Android圖片處理教程之全景查看效果實(shí)現(xiàn)
- Android仿百度圖片查看功能
- Android 簡(jiǎn)單的圖片查看器源碼實(shí)現(xiàn)
- android自定義Camera拍照并查看圖片
- Android 通過網(wǎng)絡(luò)圖片路徑查看圖片實(shí)例詳解
- android網(wǎng)絡(luò)圖片查看器簡(jiǎn)單實(shí)現(xiàn)代碼
- Android 實(shí)現(xiàn)WebView點(diǎn)擊圖片查看大圖列表及圖片保存功能
- Android實(shí)現(xiàn)圖片查看功能
相關(guān)文章
Android單項(xiàng)綁定MVVM項(xiàng)目模板的方法
這篇文章主要給大家介紹了關(guān)于Android單項(xiàng)綁定MVVM項(xiàng)目模板的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Android 列表選擇框 Spinner詳解及實(shí)例
這篇文章主要介紹了Android 列表選擇框 Spinner詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android自動(dòng)化如何獲取視圖元素屬性(最新推薦)
在做Android自動(dòng)化時(shí)候,我們需要知道視圖有哪些元素,元素都有哪些屬性,獲取到屬性我們才能獲取到元素從而做自動(dòng)化控制,所以做Android自動(dòng)化獲取元素屬性是必要的第一步,這篇文章主要介紹了Android自動(dòng)化如何獲取視圖元素屬性(最新推薦),需要的朋友可以參考下2024-07-07
android編程實(shí)現(xiàn)系統(tǒng)圖片剪裁的方法
這篇文章主要介紹了android編程實(shí)現(xiàn)系統(tǒng)圖片剪裁的方法,涉及Android針對(duì)圖片的獲取、修改、保存等操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android應(yīng)用啟動(dòng)流程之從啟動(dòng)到可交互的過程解析
這篇文章將給大家總結(jié)學(xué)習(xí)Android 基礎(chǔ)知識(shí),Android應(yīng)用啟動(dòng)流程,從啟動(dòng)到可交互的過程解析,在學(xué)習(xí)過程中,大家最好是把源碼下載下來(lái),感興趣的小伙伴跟著小編一起來(lái)看看吧2023-08-08
RecyclerVIew實(shí)現(xiàn)懸浮吸頂效果
這篇文章主要為大家詳細(xì)介紹了RecyclerVIew實(shí)現(xiàn)懸浮吸頂效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android 取消藍(lán)牙配對(duì)框?qū)崿F(xiàn)自動(dòng)配對(duì)功能
這篇文章主要介紹了Android 取消藍(lán)牙配對(duì)框?qū)崿F(xiàn)自動(dòng)配對(duì)功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02

