Android互联网访问图片并在客户端显示的方法
作者:sgx425021234 时间:2021-12-26 21:25:10
本文实例讲述了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程序设计有所帮助。
标签:Android,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
安卓实现自定义圆形取色盘
2022-01-14 18:01:55
![](https://img.aspxhome.com/file/2023/6/94276_0s.gif)
在Ubuntu中安装VSCode并配置C/C++开发环境的方法步骤
2021-07-04 18:19:19
C# ThreadPool之QueueUserWorkItem使用案例详解
2022-08-10 03:49:30
![](https://img.aspxhome.com/file/2023/3/106483_0s.png)
C#获取真实IP地址实现方法
2022-01-05 11:35:49
详谈@Autowired和static的关系
2022-06-14 05:51:50
mybatis-plus排除非表中字段的操作
2022-04-22 03:48:41
IntellJ IDEA神器使用技巧(小结)
2023-08-08 22:01:44
详解Android消息机制完整的执行流程
2021-10-14 18:11:00
Java实例讲解多态数组的使用
2021-08-30 19:34:46
![](https://img.aspxhome.com/file/2023/5/84305_0s.png)
MaterialApp Flutter 应用全局配置与主题管理详解
2023-05-03 18:50:48
mybatis-generator生成文件覆盖问题的解决
2023-03-11 15:35:01
Java Bean 作用域及它的几种类型介绍
2022-12-02 20:39:42
![](https://img.aspxhome.com/file/2023/9/75479_0s.png)
详解 c# 克隆
2021-12-16 16:23:12
![](https://img.aspxhome.com/file/2023/1/126121_0s.gif)
C#文件后缀名的详细介绍
2022-06-03 15:07:41
Java编程复用类代码详解
2021-09-13 10:06:34
C++编程异常处理中try和throw以及catch语句的用法
2023-04-08 15:29:41
Android viewpager中动态添加view并实现伪无限循环的方法
2022-11-17 12:59:08
![](https://img.aspxhome.com/file/2023/8/122108_0s.gif)
Java常用锁synchronized和ReentrantLock的区别
2023-06-01 04:42:21
![](https://img.aspxhome.com/file/2023/3/116803_0s.webp)
List集合多个复杂字段判断去重的案例
2022-08-01 16:23:28
SpringBoot如何读取xml配置bean(@ImportResource)
2021-08-03 22:33:44
![](https://img.aspxhome.com/file/2023/8/85888_0s.jpg)