1、养成好习惯,配置字符串资源文件 strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">网络图片查看器</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="imgpath">输入图片地址:</string>
<string name="getBtn">获取图片</string>
<string name="error">获取图片失败</string>
</resources>

2、布局文件,使用垂直布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/imgpath"
/> <EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imgpathInput"
android:text="http://avatar.csdn.net/B/E/7/1_gaotong2055.jpg"
android:inputType="text" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/getBtn"
android:id="@+id/getBtn"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgView"
/>
</LinearLayout>

3、编写代码

这里为了方便看代码,都写在一个类里面了。

可以把里面的静态方法单独拆分出来,写在一个工具类中,结构更好。

public class MainActivity extends Activity implements OnClickListener {
private EditText pathText;
private ImageView imageView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pathText = (EditText) this.findViewById(R.id.imgpathInput);
imageView = (ImageView) this.findViewById(R.id.imgView);
Button button = (Button) this.findViewById(R.id.getBtn);
button.setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onClick(View v) {
String path = pathText.getText().toString();
byte[] data = null;
try {
data = getImgData(path);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, R.string.error, 1).show();
}
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bitmap);
} public static byte[] getImgData(String path) throws Exception { URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);// 超时时间5秒
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream in = conn.getInputStream();
return read(in);
} else {
Log.d("tong.getImg", "服务器无响应");
} return null;
} /**
* 从一个输入流中读取数据,并返回
*
* @param in
* @return byte[] 数据
* @throws IOException
*/
public static byte[] read(InputStream in) throws IOException {
// 开辟一个内存的区域,以写入数据
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[10240];
int len = 0;
while ((len = in.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.close(); return outStream.toByteArray(); // 返回内存中的数据
} }

运行效果:

Android获取网络图片应用示例的更多相关文章

  1. Android——获取网络图片

    布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...

  2. Android获取网络图片

    /** * * 访问网络的操作,必须放在工作线程中完成 * */ public class MainActivity extends Activity { static List<HashMap ...

  3. Android · 获取网络图片

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import j ...

  4. android 获取http网络图片保存png

    1.android 获取网络图片的方式很多,普通网络通信的方式都可以用在获取网络图片上. android   http获取数据常用的方式: 1.Apache接口(HttpClient) 2.标准Jav ...

  5. URL转Drawable之 Android中获取网络图片的三种方法

    转载自: http://doinone.iteye.com/blog/1074283 Android中获取网络图片是一件耗时的操作,如果直接获取有可能会出现应用程序无响应(ANR:Applicatio ...

  6. [转]Android 如何根据网络地址获取网络图片方法

    http://blog.csdn.net/xiazdong/article/details/7724103 目录(?)[-] h2pre namecode classhtml stylefont-we ...

  7. Android 下载网络图片保存到本地

    通过网络地址获取网络图片,点击下载将图片显示出来,然后点击图片将图片保存到本地. 首先需要在manifest上添加一些权限: <!-- 访问网络的权限 --> <uses-permi ...

  8. 分享一个安卓中异步获取网络图片并自适应大小的第三方程序(来自github)

    安卓中获取网络图片,生成缓存 用安卓手机,因为手机流量的限制,所以我们在做应用时,要尽量为用户考虑,尽量少耗点用户的流量,而在应用中网络图片的显示无疑是消耗流量最大的,所以我们可以采取压缩图片或者将图 ...

  9. android下载网络图片,设置宽高,等比缩放

    使用Picasso组件去下载图片会发现图片宽高会变形不受等比缩放控制,即使设置了图片的 scaleType,可能是对Picasso的api没有用对, Picasso.with(this.activit ...

随机推荐

  1. VC 操作 EXCEL---插入工作表(Insert.Sheet)方法

    看到的资料 http://bbs.csdn.net/topics/198565 自己总结一下 //插入到nIndex工作表之前 void InsertSheet(int nIndex) { sheet ...

  2. py脚本打包exe可执行文件

    python3以上版本打包exe需要扩展软件:cx_freeze 下载地址:http://cx-freeze.sourceforge.net/ 1)安装后在\Python32\Scripts\cxfr ...

  3. CentOS使用chkconfig增加开机服务提示service xxx does not support chkconfig的问题解决

    在shell文件的第二行增加如下内容即可: # chkconfig: 2345 10 90 #服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10. # descrip ...

  4. vultr vs digitalocean vs linode

    vultr官方网站:www.vultr.comdigitalocean官方网站:www.digitalocean.comlinode官方网站:www.linode.com 一般来说我们买VPS的时候都 ...

  5. maven里如何根据不同的environment打包

    一个项目里总会有很多配置文件.而且一般都会有多套环境.开发的.测试的.正式的.而在这些不同的环境这些配置的值都会不一样.比如mail的配置.服务的url配置这些都是很常见的.所以在打包的时候就要根据e ...

  6. 聊聊JVM的年轻代(转)

    聊聊JVM的年轻代 本文转自http://ifeve.com/jvm-yong-generation/ 1.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代 ...

  7. java跨域解决

    import java.util.ArrayList; import java.util.List; import org.springframework.context.annotation.Bea ...

  8. Extjs NumberField 开始值 不能大于 结束值

    Ext.apply(Ext.form.VTypes,{ numberrange: function(val, field) { var num = parseFloat(val); if (field ...

  9. [翻译] Haneke(处理图片缓存问题)

    Haneke https://github.com/hpique/Haneke A lightweight zero-config image cache for iOS. 轻量级0配置图片缓存. H ...

  10. 使用 NSPropertyListSerialization 持久化字典与数组

    NSPropertyListSerialization The NSPropertyListSerialization class provides methods that convert prop ...