13 使用BitMap与http协议获取网络资源
来源:互联网 发布:jquery 数组去重 编辑:程序博客网 时间:2024/06/06 16:43
首先将我们的连接地址转换为BitMap资源的工具类
package org.china.java.util;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class ImageUtil {/** * 获取网络address地址对应的图片,不考虑阻塞主线程问题,主要是基本学习。 */public static Bitmap getImage(String address)throws Exception{URL url=new URL(address);HttpURLConnection conn=(HttpURLConnection) url.openConnection();//如果我们5秒钟没有得到返回数据,抛异常。conn.setRequestMethod("GET");conn.setConnectTimeout(50000);InputStream is=conn.getInputStream();byte[] imagebyte=StreamUtil.getBytes(is);Bitmap bitmap=BitmapFactory.decodeByteArray(imagebyte, 0,imagebyte.length);return bitmap;}}
将我们的InputStream转换为byte的工具类package org.china.java.util;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.InputStreamReader;public class StreamUtil {public static byte[] getBytes(InputStream is)throws Exception{ByteArrayOutputStream bos=new ByteArrayOutputStream();byte[] buffer=new byte[1024];int len=0;while((len=is.read(buffer))!=-1){bos.write(buffer, 0, len);}is.close();bos.flush();return bos.toByteArray();}@Deprecatedpublic static byte[] getByteBytes(InputStream is)throws Exception{BufferedReader buf=new BufferedReader(new InputStreamReader(is));return buf.readLine().getBytes();}}
我们很简单的activitypackage org.china.java.viewnetimage;import java.io.IOException;import java.net.SocketTimeoutException;import org.china.java.util.ImageUtil;import android.app.Activity;import android.graphics.Bitmap;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {private EditText et_address;private Button btn_view;private ImageView iv_net;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_main);this.et_address=(EditText) super.findViewById(R.id.et_address);this.btn_view=(Button) super.findViewById(R.id.btn_view);this.iv_net=(ImageView) super.findViewById(R.id.iv_net);this.btn_view.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String address=et_address.getText().toString().trim();if("".equals(address) || null== address){Toast.makeText(getApplicationContext(),"图片地址不能为空",Toast.LENGTH_SHORT).show();return ;}try {Bitmap bitmap=ImageUtil.getImage(address);iv_net.setImageBitmap(bitmap);} catch (Exception e) {if(e instanceof SocketTimeoutException){Toast.makeText(getApplicationContext(),"网络连接超时",Toast.LENGTH_LONG).show();}else if(e instanceof IOException){Toast.makeText(getApplicationContext(),"读取数据失败",Toast.LENGTH_LONG).show();}else{Toast.makeText(getApplicationContext(),"未知错误",Toast.LENGTH_LONG).show();}e.printStackTrace();}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}}最后是我们的布局文件与系统配置文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入图片地址" /> <EditText android:id="@+id/et_address" android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="2" android:text="http://f.hiphotos.baidu.com/image/w%3D2048/sign=1ff8f5cad5ca7bcb7d7bc02f8a316a63/9213b07eca8065386121681a95dda144ac3482f4.jpg" /> <Button android:id="@+id/btn_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="查看" /> <ImageView android:id="@+id/iv_net" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="center" /> </LinearLayout><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.china.java.viewnetimage" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="org.china.java.viewnetimage.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></manifest>
0 0
- 13 使用BitMap与http协议获取网络资源
- 通过HTTP获取网络资源
- 通过HTTP获取网络资源
- 使用url获取网络资源
- Jsoup 与 httpClient 获取网络资源
- Http请求、获取网络资源 、返回JsonObject
- 使用http协议获取网络图片
- 使用http协议获取网络图片
- 使用http协议获取网络图片
- 网易新闻API接口的实现(Picasso类库的使用与HTTP协议的get请求,BitMap样例(未使用))
- 获取网络资源
- 【翻译】Python如何使用urllib2获取网络资源
- Python如何使用urllib2获取网络资源
- Android使用http协议与服务器通信
- Android使用http协议与服务器通信
- Android使用http协议与服务器通信
- Android使用http协议与服务器通信
- JDK6.0学习笔记(十五)获取HTTP网络资源
- 把AXURE模型生成的HTML页面发布到互联网
- svn部署与配置
- stack的简单实现
- 汤唯被骗细节曝光 工作人员称汤唯心情平静
- Hibernate的HQL查询
- 13 使用BitMap与http协议获取网络资源
- 程序 打印 输出 调试 信息
- 字符串分割与拼接
- java 缓存OSCache介绍
- 高内聚,低耦合
- spring mvc的学习1
- Java使用java.util包中的Date类来显示本机的当前时间
- java 内存
- ubuntu 使用旧式Gnome风格的菜单