界面:

代码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上的更多相关文章

  1. Android处理Bitmap使其能够不失真等比缩放裁剪后显示在ImageView上

    Android开发过程中,我们有时需要动态得显示一些图片,并且这些图片的大小差距会十分大,如果需求并不是需要图片完整显示,但是需要不失真,并且要图片中间部分的情况下,我们需要做一系列处理,因为这个时候 ...

  2. java 通过流的方式读取本地图片并显示在jsp 页面上(类型以jpg、png等结尾的图片)

    Java代码: File filePic = new File(path+"1-ab1.png"); if(filePic.exists()){ FileInputStream i ...

  3. 在Android中如何获取视频的第一帧图片并显示在一个ImageView中

    String path  = Environment.getExternalStorageDirectory().getPath(); MediaMetadataRetriever media = n ...

  4. C# 通过URL获取图片并显示在PictureBox上的方法

    , ); System.Net.WebRequest webreq = System.Net.WebRequest.Create(url); System.Net.WebResponse webres ...

  5. 打开图片并显示在面板上demo

    import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ...

  6. 用JSP从数据库中读取图片并显示在网页上

    <1>先在mysql下建立如下的table. 并insert图像. mysql.sql文件如下: CREATE TABLE photo ( photo_no int(6) unsigned ...

  7. JSP读取数据库二进制图片并显示

    用JSP从数据库中读取二进制图片并显示在网页上 环境mysql+tomcat: 先在mysql下建立如下的表. 并存储了二进制图像(二进制格式存储图片可以参考我的另一篇博客:https://www.c ...

  8. [Android] 拍照、截图、保存并显示在ImageView控件中

    近期在做Android的项目,当中部分涉及到图像处理的内容.这里先讲述怎样调用Camera应用程序进行拍照,并截图和保存显示在ImageView控件中以及遇到的困难和解决方法.     PS:作者购买 ...

  9. .Net程序员安卓学习之路5:使用xutils注入View和事件以及图片的显示

    xUtils注入和图片显示 一.xUtils注入 引用官方介绍: ViewUtils模块: •android中的ioc框架,完全注解方式就可以进行UI,资源和事件绑定: •新的事件绑定方式,使用混淆工 ...

随机推荐

  1. The Ninth Hunan Collegiate Programming Contest (2013) Problem C

    Problem C Character Recognition? Write a program that recognizes characters. Don't worry, because yo ...

  2. DP三角形

    Hrbust1038  http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1038 // ...

  3. [ CodeVS冲杯之路 ] P1092

    不充钱,你怎么AC? 题目:http://codevs.cn/problem/1092/ 嗯,这道题有一定难度啊,需要先用扩展欧几里得算法求出逆元,然后按照大小构一颗带边权为小时数的树 树链剖分后在树 ...

  4. HTTP 错误 500(Internal Server Error)

    今天在用ajax请求页面的时候出现了这么一个错误:HTTP 错误 500(Internal Server Error) 由于提示较少,过了好一阵子才找到答案:ajax请求中调用了一个不存在的函数⊙﹏⊙ ...

  5. Android基础总结(7)——异步消息处理

    服务(Service)是Android中实现程序后台运行的解决方案,它非常适合用于去执行哪些不需要和用户交互而且还要长期运行的任务.服务的运行不依赖任何用户界面,即使当程序被切换到后台,或者用户打开了 ...

  6. Ajax-(get/post/jQuery方式请求)

    < !DOCTYPE html > < html xmlns = "http://www.w3.org/1999/xhtml" > < head &g ...

  7. [drp 4] 使用dom4j,读取XML数据,保存至数据库

    导读:上篇文章介绍了用XML文件配置数据库的连接,然后通过读取XML文件连接数据库的内容,本篇博客介绍读取XML文件,进行数据持久化的操作.PS:从某种意义上来说,经过Scheme校正的XML文件,本 ...

  8. C++ builder 操作Excel方法(据网上资料整理)

    c++ builder 操作Excel方法,下面是从网上找到的一些不错的方法,学习一下: 用OLE操作Excel(目前最全的资料)(04.2.19更新) 本文档部分资料来自互联网,大部分是ccrun( ...

  9. javascript ES5 Object对象

    原文:http://javascript.ruanyifeng.com/stdlib/object.html 目录 概述 Object对象的方法 Object() Object.keys(),Obje ...

  10. 如何使VS2008 调试网站的根目录和IIS调试的一致?

    用VS2008做asp.net网站调试时,经常会多出来一个目录,如http://localhost:1234/Foo/ , 由于一些图片的路径问题,我们不需要最后的/Foo/目录,而是像IIS调试那样 ...