关键代码:
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. Ajax的post方法,模拟 从后台读取数据小demo

    $(document).ready(function() { //定义一个函数 function timer() { $.post("1.json", function(data, ...

  2. struts2+hibernate整合开发步骤

    百度的各种代码,步骤,自己整合了一下 1,创建数据库 常用mysql   creat table..... 2,在WebContent下的bin中添加相应的包 http://pan.baidu.com ...

  3. android 单选、多选弹出菜单

    菜单单选窗口: import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInte ...

  4. 迪士尼黑科技:爬墙机器人 VertiGo

    12 月 30 日,迪士尼研发出的一款爬墙机器人曝光了一段有趣的视频.从视频里可看出这个机器人碰到墙壁时迅速地作出反应爬了上去. 据了解,这个爬墙机器人名叫 VertiGo,由迪士尼研究中心和苏黎世联 ...

  5. python3爬虫初探(四)之文件保存

    接着上面的写,抓取到网址之后,我们要把图片保存到本地,这里有几种方法都是可以的. #-----urllib.request.urlretrieve----- import urllib.request ...

  6. ASP.NET之Ajax系列(一)

    我们在Web开发中经常会接触到Ajax技术,同时Ajax技术也有很多种实现方式,那么,我们今天从第一种方式说起:ASP.NET原生控件实现Ajax. ASP.NET原生控件用于Ajax技术的主要是Up ...

  7. [转]BEHAVOUR TREE2

    上次提到了一些行为树的基本概念,包括行为节点,控制节点(选择,序列,并行),这次来更多,更深入的讨论行为树的一些东西,如果对行为树不是很了解,请参看这里. 一. 关于选择节点的讨论 我们说过选择节点的 ...

  8. C# subString的理解

    public void TestMethod1()        {            string str = "ABCDEFGHIJKLMN"; string result ...

  9. Hadoop no.1

    解决的问题: 1. 磁盘读取速度慢:磁盘容量大了,将一个大的文件存在磁盘上,但读取速度慢.    解决方法:hdfs将文件拆开存在不同的节点(datanode)上,namenode记载文件存储的位置( ...

  10. LRU

    rsms/js-lru LRU缓存介绍与实现 (Java) 使用场景 缓存计算结果