android 简单实现获取网络图片
2012-04-06 21:26
513 查看
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/imagePaht" /> <EditText android:id="@+id/imagepath" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://10.0.2.2:8080/1.png" /> <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>
mainActivity
import java.io.InputStream; 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.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; public class netImageActivity extends Activity { /** Called when the activity is first created. */ private Button button = null; private EditText pathText; private ImageView imageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); pathText = (EditText) findViewById(R.id.imagepath); button = (Button) findViewById(R.id.button); imageView = (ImageView) findViewById(R.id.imageView); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub String path = pathText.getText().toString(); Log.i("path", path); InputStream is; try { is = ImageService.getImage(path); //获取图片数据流 Bitmap bitMap = BitmapFactory.decodeStream(is); imageView.setImageBitmap(bitMap); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }
public static InputStream getImage(String path) throws Exception { // TODO Auto-generated method stub URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); return conn.getInputStream(); }
相关文章推荐
- android获取网络图片简单实现
- JAVA 实现简单的网络爬虫,获取网站图片
- Android中简单实现从网络下在图片显示并保存在本地
- Android ViewPager从网络获取图片实现无限轮播
- Android编程简单获取网络上的图片
- Android上实现一个简单的天气预报APP(三) 获取网络数据
- Android实现获取本机中所有图片(Loader,CursorLoader,LoaderManager,SimpleCursorAdapter的简单应用)
- android获取拍照图片、本地图片简单实现!
- android利用Http协议实现从网络上面获取图片的方法
- Android实现从网络获取图片显示并保存到SD卡的方法
- Android中ConvenientBanner的使用--获取网络图片 --(实现效果是自动轮播图片)
- 获取网络上的图片,并显示在界面上的简单实现
- Android中AsyncTask的简单用法及实现网络图片下载的实例
- Android仿人人客户端(v5.7.1)——对从服务器端(网络)获取的图片进行本地双缓存处理(编码实现)
- android获取拍照图片、本地图片简单实现!
- Android程序如何实现从网络中获取一张图片
- RxAndroid的简单应用——实现网络图片的下载
- android学习笔记16--------------Android实现网络图片与html源码获取
- android网络图片查看器简单实现代码
- 图片、缓存-Android仿人人客户端(v5.7.1)——对从服务器端(网络)获取的图片进行本地双缓存处理(编码实现)-by小雨