android从网络中获得一张图片,并显示在屏幕上
2011-07-20 11:50
549 查看
1:androidmanifest.xml的内容
2:MainActivity的内容
3:ImageService类的内容
4:StreamTool的内容
5:程序中用到的字符串资源strings.xml里的内容
6:程序布局文件main.xml的内容
7:程序界面及测试结果
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.capinftotech.image" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET" /> </manifest>注意访问网络中的数据需要添加android.permission.INTERNET权限
2:MainActivity的内容
package cn.capinftotech.image; import java.io.IOException; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import com.capinfotech.service.ImageService; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private EditText urlPath = null; private Button button = null; private ImageView imageView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); urlPath = (EditText)findViewById(R.id.urlpath); button = (Button)findViewById(R.id.button); imageView = (ImageView)findViewById(R.id.imageView); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String urlPathContent = urlPath.getText().toString(); try { byte[] data = ImageService.getImage(urlPathContent); Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); //生成位图 imageView.setImageBitmap(bitmap); //显示图片 } catch (IOException e) { Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_LONG).show(); //通知用户连接超时信息 Log.i(TAG, e.toString()); } } }); } }
3:ImageService类的内容
package com.capinfotech.service; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import com.capinfotech.utils.StreamTool; public class ImageService { public static byte[] getImage(String path) throws IOException { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); //设置请求方法为GET conn.setReadTimeout(5*1000); //设置请求过时时间为5秒 InputStream inputStream = conn.getInputStream(); //通过输入流获得图片数据 byte[] data = StreamTool.readInputStream(inputStream); //获得图片的二进制数据 return data; } }
4:StreamTool的内容
package com.capinfotech.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class StreamTool { /* * 从数据流中获得数据 */ public static byte[] readInputStream(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while((len = inputStream.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.close(); return bos.toByteArray(); } }
5:程序中用到的字符串资源strings.xml里的内容
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">图片浏览器</string> <string name="urlpath">网络图片地址</string> <string name="button">显示</string> <string name="error">网络连接超时</string> </resources>
6:程序布局文件main.xml的内容
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/urlpath" /> <EditText android:id="@+id/urlpath" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://www.eoeandroid.com/data/attachment/forum/201107/18/142935bbi8d3zpf3d0dd7z.jpg" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
7:程序界面及测试结果
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/08/42261704594ab67adfaefa4474f86b0d.gif)
相关文章推荐
- Android从网络中获得一张图片并显示在屏幕上的实例详解
- android从网络中获得一张图片,并显示在屏幕上
- android 网络图片按原始比例显示在屏幕上
- android显示一张大的网络图片,避免OOM
- Android中从网络上异步下载图片并显示到手机屏幕上
- android 点击一个按钮,选择一张图片,获取路径,然后显示在屏幕
- Android中Textview显示带html文本三-------【Textview显示网络图片】
- 通过异步任务将一张由网络得来图片显示出来
- Android显示网络图片(笔记)
- android中Picasso显示网络图片
- 一张图让你看懂Android不同密度屏幕图片的像素制作比例
- Android中用BaseAdapter实现带Button和显示网络图片的Listview
- android-Picasso请求https加载网络图片不能显示的解决方案
- 在android中显示网络图片及查看页面源代码
- Android显示从网络下载图片偏小的问题
- [Android实例] Android ListView从网络获取图片及文字显示 + 自己搭建的服务器的xml文件
- android屏幕单位----真机与模拟器获取同一图片显示大小不一样
- Android显示网络图片实例
- android从网络中获取一张图片
- 关于Android圆形图片的一种优化方案(可以显示网络图片)