android 图片查看器 点击删除,安卓网络图片查看器

安卓网络图片查看器

安卓

制作一个网络图片查看器http请求图片的流程http get请求

服务端返回数据 2进制流的方式代码实现步骤URL url = new URL(path);

通过url打开一个对应的连接httpUrlConnection();

设置一些初始化的参数,请求方法(GET,POST)请求头

获取服务器端响应conn.getResponseCode()

返回200 OK 400 资源找不到 503 服务器内部错误

com。getInputStream();服务器返回的数据量流因为需要访问网络,所以AndroidManifest.xml中需要添加网络权限

android stusio添加网络权限

网络在主线程的异常

NetWorkOnMainThreadException从android4.0开始,为了让应用程序更加流畅,google强制要求访问网络的操作只能在子线程

activity的onCreate方法和点击事件这些方法都是运行在主线程里面(UI线程)只有主线程可以修改应用程序的ui,其他线程修改的界面是不运行的

目录结构manifests

AndroidManifest.xml

java

Mainactivity

layout

activity_main

Mainactivitypublic class MainActivity extends AppCompatActivity {

private EditText et_image;

private ImageView iv;

//这里导包需要导入andoird.os中的包

private Handler handler = new Handler(){

//在这里ctrl+o可以快速重写方法,选择handleMessage

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

//用Bitmap接收msg

Bitmap bitmap = (Bitmap) msg.obj;

iv.setImageBitmap(bitmap);

}

};

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

et_image = (EditText) findViewById(R.id.et_image);

iv = (ImageView) findViewById(R.id.iv);

}

}

//查看图片

public void image(View view){

final String path = et_image.getText().toString().trim();

if(TextUtils.isEmpty(path)){

Toast.makeText(this,"图片路径不能为空",Toast.LENGTH_SHORT).show();

}else {

//安卓4.0以后的UI修改只能在子线程进行

//所以新建一个子线程

//new Thread(){}.start();

new Thread(){

@Override

public void run() {

//下载网络上的图片,显示到imageView里

try {

//1.创建URL对象

URL url = new URL(path);

//2.通过url对象打开http连接

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

//conn设置请求方式为GET

conn.setRequestMethod("GET");

conn.setRequestProperty("Accept","text/php,*.png,*/*");

int code = conn.getResponseCode();

//200 ok 404文件不存在或者503服务器内部错误

if(code == 200){

//得到服务器返回的数据流

InputStream is = conn.getInputStream();

Bitmap bitmap = BitmapFactory.decodeStream(is);

//iv.setImageBitmap(bitmap);

//由于子线程不可以直接修改ui

//这里是先获取数据后,调用主线程的handler发消息去更新ui

Message msg = new Message();

//把获取到的bitmap放到msg盒子里

msg.obj = bitmap;

handler.sendMessage(msg);

}else {

}

} catch (IOException e) {

e.printStackTrace();

}

}

}.start();

}

}

}

activity_main.xml<?xml version="1.0" encoding="utf-8"?>

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="cn.guotianyu.musicplayer.MainActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/et_image"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1.5"

android:hint="请输入网络图片路径"

android:text="http://guotianyu.cn:211/mouse.png"/>

android:id="@+id/bt_image"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="image"

android:text="查看"/>

android:id="@+id/iv"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#ff00ff"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值