Android之网络操作 - 从网络获取图片或网页

本文介绍如何在Java和Android应用中加载网络图片,并展示具体的实现代码。文章首先讲解了在Java环境中通过URL获取网络图片并保存的方法,接着介绍了Android应用中通过自定义组件展示网络图片的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在Java中操作显示网络图片

public class ImageRequest { /** * @param args */ public static void main(String[] args) throws Exception { //构造一个URL对象 URL url = new URL("http://www.baidu.com/img/baidu_sylogo1.gif"); //使用openConnection打开URL对象 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //使用Http协议,设置请求方式为GET conn.setRequestMethod("GET"); //设置链接超时异常,5s conn.setConnectTimeout(5 * 1000); //通过输入流获取图片数据 InputStream inStream = conn.getInputStream(); //获取到图片的二进制数据 byte[] data = readInputStream(inStream); //构造一个文件,保存图片到项目的根目录下 File imageFile = new File("baidu_logo.jpg"); //构造一个文件输出流FileOutputStream FileOutputStream outStream = new FileOutputStream(imageFile); //把文件数据写入到输出流 outStream.write(data); outStream.close(); } /** * 从输入流里面得到返回为二进制的数据 * @param inStream 输入流 * @return byte[] 二进制数据 * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception { //构造一个ByteArrayOutputStream ByteArrayOutputStream outStream = new ByteArrayOutputStream(); //设置一个缓冲区 byte[] buffer = new byte[1024]; int len = 0; //判断输入流长度是否等于-1,即非空 while( (len=inStream.read(buffer)) != -1 ) { //把缓冲区的内容写入到输出流中,从0开始读取,长度为len outStream.write(buffer, 0, len); } //关闭输入流 inStream.close(); return outStream.toByteArray(); } }

2.Android中显示图片:

(1)ImageShowActivity.java

public class ImageShowActivity extends Activity { private static final String TAG = "ImageShowActivity"; private EditText pathText; private ImageView imageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //得到EditText,ImageView与Button pathText = (EditText) this.findViewById(R.id.urlpath); imageView = (ImageView) this.findViewById(R.id.imageView); Button button = (Button)this.findViewById(R.id.button); //设置Button监听 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取EditText里面的地址 String path = pathText.getText().toString(); try { //得到图片的二进制数据 byte[] data = ImageService.getImage(path); //图片处理 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//生成位图 //显示图片 imageView.setImageBitmap(bitmap); } catch (Exception e) { //出错的时候提示错误信息 Toast.makeText(ImageShowActivity.this, R.string.error, 1).show(); //Log里面打印错误信息 Log.e(TAG, e.toString()); } } }); } }

(2)编写一个流处理工具类,StreamTool.java

public class StreamTool { /** * 从输入流里面得到返回为二进制的数据 * @param inStream 输入流 * @return byte[] 二进制数据 * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception { //构造一个ByteArrayOutputStream ByteArrayOutputStream outStream = new ByteArrayOutputStream(); //设置一个缓冲区 byte[] buffer = new byte[1024]; int len = 0; //判断输入流长度是否等于-1,即非空 while( (len=inStream.read(buffer)) != -1 ) { //把缓冲区的内容写入到输出流中,从0开始读取,长度为len outStream.write(buffer, 0, len); } //关闭输入流 inStream.close(); return outStream.toByteArray(); } }

(3)编写一个图片处理类,打开URL,获取输入流等操作

public class ImageService { public static byte[] getImage(String path) throws Exception { //构造一个URL对象 URL url = new URL("http://www.baidu.com/img/baidu_sylogo1.gif"); //使用openConnection打开URL对象 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //使用Http协议,设置请求方式为GET conn.setRequestMethod("GET"); //设置链接超时异常,5s conn.setConnectTimeout(5 * 1000); //通过输入流获取图片数据 InputStream inStream = conn.getInputStream(); //返回图片的二进制数据 return StreamTool.readInputStream(inStream); } }

注意别忘记在AndroidManifest.xml文件中添加访问网络的权限:

<uses-permission android:name="android.permission.INTERNET"/>

3.在Android中显示网页代码:通过滚动条视图(ScrollView)工具显示代码

(1)main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textView" /> </ScrollView> </LinearLayout>

(2)与上面显示图片类似

public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView textView = (TextView)this.findViewById(R.id.textView); try { textView.setText(HtmlService.getHtml("http://www.sohu.com")); } catch (Exception e) { Log.e("MainActivity", e.toString()); Toast.makeText(MainActivity.this, "网络连接失败", 1).show(); } } }

public class HtmlService { public static String getHtml(String path) throws Exception { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); //通过输入流获取html数据 InputStream inStream = conn.getInputStream(); //得到html的二进制数据 byte[] data = StreamTool.readInputStream(inStream); String html = new String(data, "gb2312"); return html; } }

public class StreamTool { /** * 从输入流中获取数据 * @param inStream 输入流 * @return * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while( (len=inStream.read(buffer)) != -1 ) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值