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. Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  2. bmp图像数据转jpg图像的C语言实现

    bmp图像格式协议 jpg图像格式协议 Bmp转jpg图像代码 密码:nmaj

  3. react父子组件数据传递

    子传父 1.首先父组件设定一个自定义函数 getChildDatas = (values) => { //...业务代码 } 2.将该函数暴露在子组件的引用上 <Child getChil ...

  4. Git提交空文件夹的技巧

    这个只能说是技巧不能说是方法,原理是在每个空文件夹新建一个.gitignore文件,然后提交. 快捷命令: find . -type d -empty -exec touch {}/.gitignor ...

  5. [Android] 字体使用dp单位避免设置系统字体大小对排版的影响

    [Android] 字体使用dp单位避免设置系统字体大小对排版的影响 以魄族mx3为例,在设置->显示->字体大小中能够选择字号大小例如以下图: 图1. 魄族mx3 会导致软件在有固定定高 ...

  6. spring整合mybatis步骤分析

    1.spring配置datasource bean的时候,不同的数据库连接方式有有不同的datasource实现类. 比如采用c3p0数据库连接池,要用c3p0的datasource实现类 com.m ...

  7. js自动补全实例

    var oInputField ,oPopDiv , oColorsUl,aColors; //初始化变量 function initVars(modelId,divId,ulId){ oInputF ...

  8. PyCharm中Python代码提示:Shadows name from outer scope

    函数内部的变量,如果和函数被调用的外部的变量一样的话,就被PyCharm中叫做shadows name 这样的话,容易引发不容易觉察到的,由于函数内部和外部的变量名一致而引发的一些问题: 比如:内部函 ...

  9. maven+jenkins+jmeter性能测试:maven把项目依赖拷贝到项目指定位置

    先上pom.xml配置: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...

  10. scala编程第16章学习笔记(4)——List对象的方法

    通过元素创建列表:List.apply List(1, 2, 3) 等价于List.apply(1, 2, 3): scala> List.apply(1, 2, 3) res0: List[I ...