关键代码:
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
imageview.setImageBitmap(bitmap);

注意访问网络权限:<uses-permission android:name="android.permission.INTERNET"/>

完整测试代码如下:

 package caicai.cn.netimag;

  import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class imageserver {
public static byte[] getimage(String path) throws Exception{ //连接远程网址
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
InputStream instream=conn.getInputStream();
return read(instream);
}
return null;
} private static byte[] read(InputStream instream) throws Exception{ // 读取数据流,返回字节数据流
ByteArrayOutputStream outstream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
while( (instream.read(buffer))!=-1){
outstream.write(buffer);
}
instream.close();
return outstream.toByteArray();
}
}

imageserver.java

 package caicai.cn.netimag;

  import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast; public class NetimageActivity extends Activity { public ImageView imageview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview=(ImageView) findViewById(R.id.imageview);
}
public void submit(View v){
String path="http://192.168.0.117/testxml/bottom.png";
try{
byte[] data=imageserver.getimage(path);
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length); //生成图片工厂
imageview.setImageBitmap(bitmap); //显示图片
}catch(Exception e){ Toast.makeText(getApplicationContext(), "出错了", 1).show();
}
}
}

NetimageActivity.java

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="获取网络图片"
android:onClick="submit"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageview"
android:src="@drawable/ic_launcher"
/> </LinearLayout>

main.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="caicai.cn.netimag"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".NetimageActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

AndroidManifest.xml

更新最新加载方法

	/**
* 根据一个网络连接(String)获取bitmap图像
*
* @param imageUri
* @return
* @throws MalformedURLException
*/
public static Bitmap getbitmap(String imageUri) {
// 显示网络上的图片
Bitmap bitmap = null;
try {
URL myFileUrl = new URL(imageUri);
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return bitmap;
}

android读取远程图片案例的更多相关文章

  1. android读取大图片并缓存

    最近开发电视版的云存储应用,要求”我的相册“模块有全屏预览图片的功能,全屏分辨率是1920*1080超清.UI组件方面采用Gallery+ImageSwitcher组合,这里略过,详情参见google ...

  2. Android读取url图片保存及文件读取

    参考: 1.http://blog.csdn.net/ameyume/article/details/6528205 2.http://blog.sina.com.cn/s/blog_85b3a161 ...

  3. Mono for Android 显示远程图片

    Main.axml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  4. 【原创】Android 4.4前后版本读取图库图片方式的变化

    Android 4.4前后版本读取图库图片方式的变化   本文讲述Android 4.4(KitKat)前后访问图库以及访问后通过图片路径读取图片的变化   Android 4.4(KitKat)以前 ...

  5. Android远程图片获取和本地缓存

    对于客户端——服务器端应用,从远程获取图片算是经常要用的一个功能,而图片资源往往会消耗比较大的流量,对 应用来说,如果处理不好这个问题,那会让用户很崩溃,不知不觉手机流量就用完了,等用户发现是你的应用 ...

  6. java读取远程url图片,得到宽高

    链接地址:http://blog.sina.com.cn/s/blog_407a68fc0100nrb6.html import java.io.IOException;import java.awt ...

  7. Android 简单图片浏览器 读取sdcard图片+形成缩略图+Gallery

    1.读取SD卡上面的图片信息 //想要的返回值所在的列 String[] projection = { MediaStore.Images.Thumbnails._ID}; //图片信息存储在 and ...

  8. tensorflow读取图片案例

    1.知识点 """ 1.图片读取流程与API: 1.构造图片文件队列 文件队列API: a)tf.train.string_input_producer(string_t ...

  9. 火车头dede采集接口,图片加水印,远程图片本地化,远程无后缀的无图片本地化

    <?php /* [LocoySpider] (C)2005-2010 Lewell Inc. 火车采集器 DedeCMS 5.7 UTF8 文章发布接口 Update content: 图片加 ...

随机推荐

  1. Unknown type name “CGFloat

    一.编绎显示Unknown type name “CGFloat”  错误解决方法 将Compile Sources As 改为 Objective-C++ 二.如果是extern const引起的. ...

  2. 用javascript正则表达式来格式化金额

    <html><head><script> function a() { var amount = "-22334.334455"; //if(/ ...

  3. nginx添加未编译安装模块

    链接:http://taokey.blog.51cto.com/4633273/1318719

  4. php大力力 [021节]mysql表名在mac下不能大写

    2015-08-27 php大力力021.mysql表名在mac下不能大写 刚才数据库里面,phpMyAdmin狂出错. Some errors have been detected on the s ...

  5. python3爬虫初探(五)之从爬取到保存

    想一想,还是写个完整的代码,总结一下前面学的吧. import requests import re # 获取网页源码 url = 'http://www.ivsky.com/tupian/xiaoh ...

  6. socket编程(Linux)

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

  7. ERP PowerDesigner工具使用(二)

    工具简介:

  8. PHP 调用oracle存储过程

    $orderdate = strtotime($this->input->get('orderdate')); $today = strtotime(date('Y-m-d',time() ...

  9. 使用Inno Setup 打包.NET程序,并自动安装.Net Framework

    使用Inno Setup 打包.NET程序,并自动安装.Net Framework http://www.cnblogs.com/xiaogangqq123/archive/2012/03/19/24 ...

  10. 转:设置session过期时间

    在Asp.net应用中,很多人会遇到Session过期设置有冲突.其中,可以有四处设置Session的过期时间: 一.全局网站(即服务器)级 IIS-网站-属性-Asp.net-编辑配置-状态管理-会 ...