android 通过网络图片路径查看图片
2013-04-20 16:01
399 查看
1.在项目清单中添加网络访问权限
<!--访问网络的权限--> <uses-permission android:name="android.permission.INTERNET"/>2.获取网络图片数据
/** * 获取网络图片的数据 * @param path 网络图片路径 * @return * @throws Exception */ public static byte[] getImage(String path) throws Exception { URL url=new URL(path); HttpURLConnection conn=(HttpURLConnection)url.openConnection();//得到基于HTTP协议的连接对象 conn.setConnectTimeout(5000);//设置超时时间 conn.setRequestMethod("GET");//请求方式 if(conn.getResponseCode()==200){//判断是否请求成功 InputStream inputStream=conn.getInputStream(); return read(inputStream); } return null; } /** * 读取流中的数据 */ public static byte[] read(InputStream inputStream) throws IOException { ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); byte[] b=new byte[1024]; int len=0; while((len=inputStream.read(b))!=-1){ outputStream.write(b); } inputStream.close(); return outputStream.toByteArray(); }3.处理查看图片的控制
public class NetimageActivity extends Activity { private EditText pathText; private ImageView imageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); pathText=(EditText)this.findViewById(R.id.imagepath);//图片路径 imageView=(ImageView)this.findViewById(R.id.imageView);//显示图片控件 Button button=(Button)this.findViewById(R.id.button);//查看图片按钮 button.setOnClickListener(new ButtonClickListener());//注册查看图片按钮事件 } /** * 处理查看图片按钮事件 */ private final class ButtonClickListener implements View.OnClickListener{ @Override public void onClick(View v) { //取得图片路径 String path=pathText.getText().toString(); try { //获取图片数据 byte[] data=ImageService.getImage(path); //使用数组的所有数据构建位图对象 Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length); imageView.setImageBitmap(bitmap);//显示图片 } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), R.string.error, 1).show(); } } } }
相关文章推荐
- Android 通过网络图片路径查看图片实例详解
- android通过json解析到的路径后异步加载网络图片
- 详细的android通过相册和相机获取图片和路径的方法
- [Android]通过setImageURI设置网络上面的图片
- Android入门之网络图片查看器
- Android通过HttpURLConnection类进行网络通信,获取网络图片
- Android通过手势实现图片按照指定路径移动
- android通过网络下载图片并设置为桌面
- Android--通过Http下载网络图片
- Android开发之网络图片查看方法BitmapFactory.decodeStream()学习01
- Android通过URL读取网络图片
- android开发-通过HttpURLConnection获取网络内容,Bitmap把二进制格式的图片转为位图
- android查看网络图片
- android系统通过图片绝对路径获取URI的三种方法
- android系统通过图片绝对路径获取URI的三种方法
- Android 通过开源框架实现加载网络图片并下载到SD卡通知系统相册显示,(并实现分享图片功能)
- android网络图片查看器
- android系统通过图片绝对路径获取URI的三种方法
- Android 通过图库选择图片提示找不到路径 open failed: EROFS (Read-only file system)
- Android通过手势实现图片按照指定路径移动