当前位置: 首页 > 编程笔记 >

Android互联网访问图片并在客户端显示的方法

曹疏珂
2023-03-14
本文向大家介绍Android互联网访问图片并在客户端显示的方法,包括了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程序设计有所帮助。

 类似资料:
  • 我如何让这段代码永远在后台运行,并始终检测是否有互联网接入(不是互联网连接),并在没有互联网接入时显示一个祝酒词? 这是我想要的(见喵喵的回答),但它是用于检测互联网的

  • 本文向大家介绍Android 下载网络图片并显示到本地,包括了Android 下载网络图片并显示到本地的使用技巧和注意事项,需要的朋友参考一下 Android下载网络图片的流程是: 发送网络请求->将图片以流的形式下载下来->将流转换为Bitmap并赋给ImageView控件。 注意点 最新的Android系统不可以在主线程上请求网络,需要使用线程来请求 下载图片属于耗时任务,最优做法是放在一个A

  • 问题内容: 我有一个具有多个IP地址的专用服务器,某些IP关联了mac地址,而其他IP(在子网中)则没有mac地址。我已经使用以下方法创建了docker macvlan网络: 我的IP:88.99.102.115,Mac:00:50:56:00:60:42。使用以下方法创建了一个容器: 这有效,我可以从外部访问该IP地址托管的Nginx。 IP没有MAC地址且网关不在子网中的情况。 子网:88.9

  • 因此,在Android的ImageView中显示图像非常简单。我从图库中挑选了一张图片,保存了它到数据库的链接,然后尝试在ImageView中显示它。 我也尝试了这个小的图像不是由相机拍摄的,这是工作的。但每次我选择用手机相机拍摄的图像时,都会有一个错误: 那么,在超过300万像素的相机中,如何在我的ImageView中显示图像呢?

  • 为什么网页只能正确显示一张静态图片啊 求解

  • 我正在尝试将容器部署到AWS上的专用网络Fargate集群。我的单个专有网络上确实有一个互联网网关: 我的集群/服务所在的VPC中确实有一个NAT网关,用于特定子网: 该子网的路由似乎也可以: 服务的安全组不会阻止任何传入连接: 但我的容器甚至没有以臭名昭著的异常开始:CannotPullContainerError:来自守护进程的错误响应:Gethttps://registry-name/:ne