通过URLHttpConnection方式来取得图片,并且显示在ImageView上
界面:
代码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="com.zzw.watctImage.MainActivity" > <ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:src="@drawable/t01a7b264978bb316f1"
android:layout_height="wrap_content"
android:layout_weight="1" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <EditText
android:id="@+id/url_edit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:singleLine="true"
android:text="http://p1.so.qhimg.com/t017cf2863df563308b.jpg" /> <Button
android:id="@+id/get"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="获得图片"
android:textColor="@android:color/holo_blue_light" />
</LinearLayout> </LinearLayout>
XML
java代码中通过点击获得图片按钮得到编辑好的URL的图片
java代码:
package com.zzw.watctImage; import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener {
ImageView image_view = null;
EditText image_uri = null;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if (msg.what == 0) {
Bitmap bitmap = (Bitmap) msg.obj;
image_view.setImageBitmap(bitmap);
} } };
/**
* 1.访问网络不能直接放在主方法里面(android.os.NetworkOnMainThreadException), 应该放在一个线程里面
* 2.android.view.ViewRootImpl$CalledFromWrongThreadException:
* 只能在主线程或者UI线程里面修改视图,应该用通信来解决
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
} // 初始化
public void init() {
image_view = (ImageView) findViewById(R.id.image_view);
image_uri = (EditText) findViewById(R.id.url_edit);
findViewById(R.id.get).setOnClickListener(this);
} @Override
public void onClick(View v) {
final String uri = image_uri.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Bitmap bitmap = getBitmap(uri);
if (bitmap != null) {
Message msg = new Message();
msg.what = 0;// 说明你是谁
msg.obj = bitmap;
handler.sendMessage(msg);
} }
}).start();
;
} // bitmap------>位图
public Bitmap getBitmap(String uri) {
HttpURLConnection conn = null;
try {
// 1、获得图片的url
URL url = new URL(uri); // 2、获得网络连接
conn = (HttpURLConnection) url.openConnection(); // 3、设置请求的一些常用参数
conn.setReadTimeout(3000);// 设置连接去读取数据的最长时间
conn.setConnectTimeout(3000);// 设置超时
conn.setDoInput(true);// 设置请求可以让服务器写入数据 // 4、真正的请求图片,然后把从服务器上请求的二进制流保存到inputStream里面
conn.connect();
InputStream in = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
return bitmap;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 5、关闭网络连接
finally {
if (conn != null) {
conn.disconnect();
}
}
return null;
}
}
JAVA
通过URLHttpConnection方式来取得图片,并且显示在ImageView上的更多相关文章
- Android处理Bitmap使其能够不失真等比缩放裁剪后显示在ImageView上
Android开发过程中,我们有时需要动态得显示一些图片,并且这些图片的大小差距会十分大,如果需求并不是需要图片完整显示,但是需要不失真,并且要图片中间部分的情况下,我们需要做一系列处理,因为这个时候 ...
- java 通过流的方式读取本地图片并显示在jsp 页面上(类型以jpg、png等结尾的图片)
Java代码: File filePic = new File(path+"1-ab1.png"); if(filePic.exists()){ FileInputStream i ...
- 在Android中如何获取视频的第一帧图片并显示在一个ImageView中
String path = Environment.getExternalStorageDirectory().getPath(); MediaMetadataRetriever media = n ...
- C# 通过URL获取图片并显示在PictureBox上的方法
, ); System.Net.WebRequest webreq = System.Net.WebRequest.Create(url); System.Net.WebResponse webres ...
- 打开图片并显示在面板上demo
import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ...
- 用JSP从数据库中读取图片并显示在网页上
<1>先在mysql下建立如下的table. 并insert图像. mysql.sql文件如下: CREATE TABLE photo ( photo_no int(6) unsigned ...
- JSP读取数据库二进制图片并显示
用JSP从数据库中读取二进制图片并显示在网页上 环境mysql+tomcat: 先在mysql下建立如下的表. 并存储了二进制图像(二进制格式存储图片可以参考我的另一篇博客:https://www.c ...
- [Android] 拍照、截图、保存并显示在ImageView控件中
近期在做Android的项目,当中部分涉及到图像处理的内容.这里先讲述怎样调用Camera应用程序进行拍照,并截图和保存显示在ImageView控件中以及遇到的困难和解决方法. PS:作者购买 ...
- .Net程序员安卓学习之路5:使用xutils注入View和事件以及图片的显示
xUtils注入和图片显示 一.xUtils注入 引用官方介绍: ViewUtils模块: •android中的ioc框架,完全注解方式就可以进行UI,资源和事件绑定: •新的事件绑定方式,使用混淆工 ...
随机推荐
- ReverseBits
eclipse没问题,leetcode 1不能通过,超出int最大值了,但是怎么转无符号? /*Write a function that takes an unsigned integer and ...
- springMVC之HelloWorld
一.总结 1.web项目一定要把引用的jar包放在WEB-INF/lib下(common-logging1.2,spring4.1.6所有包,其实不需要那么多,懒得筛选了,) 2.web.xml中要初 ...
- What is the Database Initialization Parameter That is Associated to an ORA-32004 Error ?
APPLIES TO: Oracle Database - Enterprise Edition - Version 9.2.0.1 to 11.2.0.3 [Release 9.2 to 11.2] ...
- 使用 Sahi 实现 Web 自动化测试
Sahi 是 Tyto Software 旗下的一个基于业务的开源 Web 应用自动化测试工具.Sahi 运行为一个代理服务器,并通过注入 JavaScript 来访问 Web 页面中的元素.Sahi ...
- <路径算法>哈密顿路径变种问题(2016华为软件精英挑战赛初赛)
原创博客,转载请联系博主! 前言:几天前华为的这个软件精英(算法外包)挑战赛初赛刚刚落幕,其实这次是我第二次参加,只不过去年只入围到了64强(32强是复赛线),最后搞到了一个华为的一顶帽子(感谢交大l ...
- Spring缓存注解@Cache使用
参考资料 http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ http://swiftlet.net/archive ...
- phonegap/cordova 升级版本
调用语句 : windows用户 npm update -g cordova 如果是 mac系统的用户 使用: 查看cordova信息 npm info cordova 查看cordova 版本 查 ...
- oracle误删除恢复
create table first_fill_20151207 as -- 生成到临时表select * from first_fillas of timestamp to_timestamp('2 ...
- django apache error.log过大
利用apache运行django框架,发现apache中error.log增长迅猛,寻找原因不得,于是手动清空... 但是当server遇到问题时,文件过大,导致定位问题十分不便 于是决定探个究竟 首 ...
- 如何利用百度地图JSAPI画带箭头的线?
百度地图JSAPI提供两种绘制多折线的方式,一种是已知多折线经纬度坐标串通过AddOverlay接口进行添加:另一种是通过在地图上鼠标单击进行绘制(鼠标绘制工具条库).目前这两种方式只能绘制多折线,并 ...