互联网访问图片,在android客户端显示
来源:互联网 发布:淘宝需要交多少钱 编辑:程序博客网 时间:2024/05/22 02:27
1、布局界面
<RelativeLayout 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: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" > <EditText android:id="@+id/url_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:ems="10" android:inputType="textPostalAddress" android:text="@string/url_text" > <requestFocus /> </EditText> <Button android:id="@+id/btn_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/url_text" android:layout_below="@+id/url_text" android:layout_marginTop="32dp" android:onClick="sendHttp" android:text="@string/btn_text" /> <ImageView android:id="@+id/iv_ie" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/url_text" android:layout_below="@+id/btn_text" android:src="@drawable/ic_launcher" /></RelativeLayout>
2、封转的一些类
URL的封装:
package com.example.lession08_code.utis;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class HttpUtils {public static String sendGet(String path){String content=null;try{//设置访问的urlURL url=new URL(path);//打开请求HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();//设置请求的信息httpURLConnection.setRequestMethod("GET");//设置请求是否超时httpURLConnection.setConnectTimeout(5000);//判断服务器是否响应成功if(httpURLConnection.getResponseCode()==200){//获取响应的输入流对象InputStream is=httpURLConnection.getInputStream();byte data[]=StreamTools.isTodata(is);//把转换成字符串content=new String(data);//内容编码方式if(content.contains("gb2312")){content=new String(data,"gb2312");}}//断开连接httpURLConnection.disconnect();}catch(Exception e){e.printStackTrace();}return content;}public static Bitmap sendGets(String path){Bitmap bitmap=null;try{//设置访问的urlURL url=new URL(path);//打开请求HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();//设置请求的信息httpURLConnection.setRequestMethod("GET");//设置请求是否超时httpURLConnection.setConnectTimeout(5000);//判断服务器是否响应成功if(httpURLConnection.getResponseCode()==200){//获取响应的输入流对象InputStream is=httpURLConnection.getInputStream();//直接把is的流转换成Bitmap对象bitmap=BitmapFactory.decodeStream(is);}//断开连接httpURLConnection.disconnect();}catch(Exception e){e.printStackTrace();}return bitmap;}}
判断网络是否连接的封装类
package com.example.lession08_code.utis;import android.app.AlertDialog;import android.content.ComponentName;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.widget.Toast;public class NetWorkUtils {private Context context;// 网路链接管理对象public ConnectivityManager connectivityManager;public NetWorkUtils(Context context) {this.context = context;// 获取网络链接的对象connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);}public boolean setActiveNetWork() {boolean flag=false;// 获取可用的网络链接对象NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();if (networkInfo == null) {new AlertDialog.Builder(context).setTitle("网络不可用").setMessage("可以设置网络?").setPositiveButton("确认",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {Toast.makeText(context, "点击确认",Toast.LENGTH_LONG).show();// 声明意图Intent intent = new Intent();intent.setAction(Intent.ACTION_MAIN);intent.addCategory("android.intent.category.LAUNCHER");intent.setComponent(new ComponentName("com.android.settings","com.android.settings.Settings"));intent.setFlags(0x10200000);// 执行意图context.startActivity(intent);}}).setNegativeButton("取消",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {}}).show();// 必须.show();}if(networkInfo!=null){flag=true;}return flag;}}
输出流的封装类
package com.example.lession08_code.utis;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;public class StreamTools {public static byte[] isTodata(InputStream is) throws IOException{//字节输出流ByteArrayOutputStream bops=new ByteArrayOutputStream();//读取数据的缓冲区byte buffer[]=new byte[1024];//读取记录的长度int len=0;while((len=is.read(buffer))!=-1){bops.write(buffer, 0, len);}//把读取的内容转换成byte数组byte data[]=bops.toByteArray();return data;}}
注意:在这里还需要加权限问题
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/>
- 互联网访问图片,在android客户端显示
- Android开发(13)-- 互联网访问图片,在android客户端显示
- Android开发(13)-- 互联网访问图片,在android客户端显示
- android客户端点击图片,显示密码可见
- 一步一步学会http获取tomcat服务端的图片,在android客户端显示
- 获取互联网上的图片并显示在界面上
- Python服务端,通过TCP发送一张图片给Android客户端,客户端把图片显示出来
- Android本地选择图片显示并上传(客户端+服务器)
- 上传图片在客户端用Javascript做图片检验,并显示图片缩略图
- Android 在程序界面上显示图片
- 如何在Android当中显示网络图片
- 如何在Android当中显示网络图片
- Android 在程序界面上显示图片
- 如何在Android当中显示网络图片
- 在Android程序里显示网络图片
- 如何在Android当中显示网络图片
- Android如何在TextView中显示图片
- 如何在Android当中显示网络图片
- 设计模式-外观模式
- 数据库系统的工作流程
- shp文件导入mysql的方法总结
- TP中验证码加载不上
- 飞思卡尔IMX6处理器的GPIO配置方式
- 互联网访问图片,在android客户端显示
- Android在发送请求时的空格问题
- 阶段性迷茫中
- 正则表达式(regular expression)
- 字符集不同引发的MySQL的1366错误:Warning: #1366 Incorrect string value...
- Ubuntu 下格式化 分区U盘SD 卡
- jquery dialog异常 Uncaught TypeError: Object [object Object] has no method 'dialog'
- SED 中的正则表达式
- 第49天的交易(2013-6-5)(-100)(-365.34)