网络图片浏览器
来源:互联网 发布:淘宝一心店铺靠谱吗 编辑:程序博客网 时间:2024/05/22 20:27
首先记住图片的网址,然后代码如下:
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1000"
android:id="@+id/iv" />
<EditText
android:singleLine="true"
android:id="@+id/et_path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入图片路径"
/>
<Button
android:onClick="click"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="浏览"/>
</LinearLayout>
public class MainActivity extends Activity {
protected static final int CHANGE_UI = 1;
protected static final int ERROR = 2;
private EditText et_path;
private ImageView iv;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == CHANGE_UI) {
System.out.println("I get the picture!");
Bitmap bitmap = (Bitmap) msg.obj;
iv.setImageBitmap(bitmap);
} else if (msg.what == ERROR) {
Toast.makeText(MainActivity.this, "显示图片错误", Toast.LENGTH_LONG).show();
}
} ;
};
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, "图片路径不能为空", Toast.LENGTH_LONG).show();
} else {
new Thread() {
private HttpURLConnection conn;
private Bitmap bitmap;
public void run() {
try {
URL url = new URL(path);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;" + "SV1;.NET4.0C;.NET4.0E;.NET CLR 2.0.50727;" + ".NET CLR 3.0.4506.2152;" +
".NET CLR 3.5.30729;Shuame)");
int code = conn.getResponseCode();
System.out.println(code);
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 (MalformedURLException e) {
e.printStackTrace();
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
}
}
}
- 网络图片浏览器
- 网络图片浏览器
- 网络图片浏览器案例
- 网络图片浏览器
- 网络图片浏览器案例
- 网络图片浏览器
- 网络图片浏览器
- 网络图片浏览器
- 网络图片浏览器
- 网络图片浏览器
- 网络图片浏览器
- 网络图片浏览器
- 网络图片浏览器
- Android--网络图片浏览器
- 网络图片浏览器
- 网络图片浏览器
- 网络图片浏览器
- 网络图片浏览器案例
- 四。初探CSS
- C#通过websevice发布到IIS 上传到服务器(转载)
- 要将"China"译成密码,密码规律是: 用原来的字母后面第 4 个字母代替原来的字母
- NYOJ-21-三个水杯(bfs搜索)
- 项目管理中通过CPM算法求关键路径,最早和最晚开始时间
- 网络图片浏览器
- Android 6.0: 动态权限管理的解决方案
- Android Retrofit的使用--基础篇
- hover对同级兄弟元素以及子集元素的操作
- 实现SSH免密登录
- Win7下 Sqli-labs 环境搭建
- 电力项目中大数据技术的典型应用
- 修改主机名
- javascript之error的属性和类型