Android互联网访问图片并在客户端显示的方法
作者:sgx425021234 发布时间:2021-12-26 21:25:10
标签:Android,图片
本文实例讲述了Android互联网访问图片并在客户端显示的方法。分享给大家供大家参考,具体如下:
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{
//设置访问的url
URL 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{
//设置访问的url
URL 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() {
@Override
public 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() {
@Override
public 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程序设计有所帮助。
0
投稿
猜你喜欢
- RestTemplate 请求url中包含百分号 会被转义成25最初使用RestTemplate 进行远程调用方法如下:private St
- 什么是雪花算法雪花算法的本质为生成一个64位长度的具有自增性的分布式全局唯一id。在64bits中,会对不同段的位进行划分。可分为:符号段时
- 前言在SpringIOC中,我们熟知的BeanScope有单例(singleton)、原型(prototype), Bean的Scope影响
- 分页实现的基本过程是这样的:1. 设置自己的分页器的基本参数(可以从配置文件中读取)■每页显示的记录条数■每次最多显示多少页2. 编写设置分
- 这篇文章主要介绍了Spring Cloud Zuul添加过滤器过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 目录问题如下:一. 解决问题一(kafka监听不到数据)二. 解决问题二(kafka为什么会有重复数据发送)三. 解决问题三(kafka数据
- 生成1. 生成error 文件的路径:你可以通过参数设置-XX:ErrorFile=/path/hs_error%p.log, 默认是在Ja
- JFrame默认的窗体比较土,可以通过一定的美化,让窗体表现的比较漂亮,具体要根据设计的设计图进行美化;JFrame美化的大致思路:先将JF
- MybatisMyBatis ,是国内最火的持久层框架采用了ORM思想解决了实体类和数据库表映射的问题。对JDBC进行了封装,屏蔽了JDBC
- 本文实例讲述了C#检测是否有u盘插入的方法。分享给大家供大家参考。具体如下:该C#代码可监控是否有u盘插入,同时可以监控其它驱动器的变化us
- 摘要: 前面介绍了MapStrut简单用法,MapStrut的最重要的特点就是处理Java中实体与模型间不匹配属性的转换。实体模型有一个Us
- 目前做的项目使用的是MAVEN来管理jar包,这也是我第一次接触maven,感觉非常好,再也不用一个一个去添加和下载jar包了,直接在mav
- 公司最近也开始基于android4.0 ICS修改框架了,公司的手机暂时不适合拿回家测试,也没有kernel的权限。从个人的角度看,我手上现
- 前言感觉Jpa的动态构建查询不好使用,然后mybatis-plus没有动态构建表的功能,有没有可能使两者混合使用,利用Jpa自动建表的优势
- 目录1. 前言2. 方法1 使用SurfaceView3. 方法2 给拍照下来的图片添加水印第一步:获取拍照权限第二步:拍照调用相机进行拍照
- 发现问题最近工作中利用JNA 调用 dll 库时保错,错误如下:///////////////// 通过 JNA 引入 DLL 库 ////
- 官方文档 https://developer.android.google.cn/guide/components/activit
- 本文实例讲述了Java实现的日期处理类。分享给大家供大家参考,具体如下:开发中常常要使用日期,先小结如下,以备后用。import java.
- 前言本篇文章讲的是Kotlin 自定义view之实现标尺控件Ruler,以选择身高、体重等。开发中,当我们需要获取用户的身高和体重等信息时,
- 前言最近在维护一个运营端的系统,和前端联调的过程中,经常需要排查一些交互上的问题,每次都得看前端代码的传参和后端代码的出参,于是打算给HTT