源码 [工程文件]:https://gitee.com/lwx001/ImageView 运行截图 : activity_main.xml : Ma
源码 [工程文件]:https://gitee.com/lwx001/ImageView
运行截图 : activity_main.xml :
MainActivity.java :
package cn.lwx.imageview;
import Androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
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 ivPic;
// 主线程创建消息处理器
private Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
if (msg.what == CHANGE_UI) {
Bitmap bitmap = (Bitmap) msg.obj;
ivPic.setImageBitmap(bitmap);
} else if (msg.what == ERROR) {
Toast.makeText(MainActivity.this, "显示图片错误 !",
Toast.LENGTH_SHORT).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_path);
ivPic = (ImageView) findViewById(R.id.iv_pic);
//et_path.setText("http://www.photophoto.cn/m6/018/030/0180300388.jpg");
//et_path.setText("图片地址!!!");
//https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2834084951,2081126851&fm=11&gp=0.jpg
}
public void click(View view) {
final String path = et_path.getText().toString().trim();
if (TextUtils.isEmpty(path)) { //判断是否为空串
Toast.makeText(this, "图片路径不能为空 !", Toast.LENGTH_SHORT).show();
} else {
//子线程请求网络,Android4.0以后访问网络不能放在主线程中
new Thread() { //开启一个线程
private HttpURLConnection conn;
private Bitmap bitmap;
public void run() { //联网操作
// 连接服务器 get 请求 获取图片
try {
// 创建URL对象
URL url = new URL(path);
// 根据url, 发送http的请求
conn = (HttpURLConnection) url.openConnection();
// 设置请求的方式
conn.setRequestMethod("GET");
// 设置超时时间
conn.setConnectTimeout(5000);
// 得到服务器返回的响应码
int code = conn.getResponseCode();
// 请求网络成功后, 返回码是200
if (code == 200) {
//获取输入流
InputStream is = conn.getInputStream();
//将流转换成Bitmap对象
bitmap = BitmapFactory.decodeStream(is);
//强制规定:必须在主线程中 更改图片
//将更改主界面的消息发送给主线程
Message msg = new Message();
msg.what = CHANGE_UI;//int型常量,区分不同的消息
msg.obj = bitmap;//通过消息携带对应的图片(object类型)
handler.sendMessage(msg);//发送消息
} else {
//返回码不等于200 请求服务器失败
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
}
//关闭连接
conn.disconnect();
}
}.start();
}
}
}
AndroidManifest.xml :
为善者,福虽未至,祸已远离~ 嘻嘻(●'◡'●)
--结束END--
本文标题: Android 网络图片浏览器( ImageView )
本文链接: https://www.lsjlt.com/news/29218.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0