在android中显示网络图片及查看页面源代码
16lz
2021-12-04
一:查看网络图片
1、用tomcat跑起一个web项目,记下图片路径:
这里的ip切记不要写成localhost或者127.0.0.1 ,写成局域网的。
2、搭建android项目
涉及的文件如下图红框内
(1)、MainActivity
/netimage/src/net/thinkeye/image/ImageMainActivity.java
- packagenet.thinkeye.image;
- importnet.thinkeye.service.ImageService;
- importandroid.os.Bundle;
- importandroid.app.Activity;
- importandroid.graphics.Bitmap;
- importandroid.graphics.BitmapFactory;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- importandroid.widget.ImageView;
- importandroid.widget.Toast;
- publicclassImageMainActivityextendsActivity{
- privateEditTextpathText;
- privateImageViewimageView;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.image_main);
- pathText=(EditText)this.findViewById(R.id.imagepath);
- imageView=(ImageView)this.findViewById(R.id.imageView);
- Buttonb=(Button)this.findViewById(R.id.button);
- b.setOnClickListener(newButtonClickListener());
- }
- privatefinalclassButtonClickListenerimplementsView.OnClickListener{
- @Override
- publicvoidonClick(Viewv){
- Stringpath=pathText.getText().toString();
- try{
- byte[]data=ImageService.getImage(path);
- Bitmapbitmap=BitmapFactory.decodeByteArray(data,0,data.length);
- imageView.setImageBitmap(bitmap);//显示图片
- }catch(Exceptione){
- e.printStackTrace();
- Toast.makeText(getApplicationContext(),R.string.error,1).show();
- }
- }
- }
- }
2、
/netimage/src/net/thinkeye/service/ImageService.java
- packagenet.thinkeye.service;
- importjava.io.InputStream;
- importjava.net.HttpURLConnection;
- importjava.net.URL;
- importnet.thinkeye.utils.StreamTool;
- publicclassImageService{
- /**
- *获取网络图片的数据
- *@parampath
- *@return
- */
- publicstaticbyte[]getImage(Stringpath)throwsException{
- URLurl=newURL(path);
- HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
- conn.setConnectTimeout(5000);
- conn.setRequestMethod("GET");
- if(conn.getResponseCode()==200){
- InputStreamis=conn.getInputStream();
- returnStreamTool.read(is);
- }
- returnnull;
- }
- }
3、/netimage/src/net/thinkeye/utils/StreamTool.java
- packagenet.thinkeye.utils;
- importjava.io.ByteArrayOutputStream;
- importjava.io.InputStream;
- publicclassStreamTool{
- /**
- *读取流中的数据
- *@paramis
- *@return
- *@throwsException
- */
- publicstaticbyte[]read(InputStreamis)throwsException{
- ByteArrayOutputStreamo=newByteArrayOutputStream();
- byte[]buffer=newbyte[1024];
- intlen=0;
- while((len=is.read(buffer))!=-1){
- o.write(buffer,0,len);
- }
- is.close();
- returno.toByteArray();
- }
- }
4、页面布局部分:
image_main.xml
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/imagepath"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="http://192.168.1.103:8888/SAMS/2.gif"
- android:id="@+id/imagepath"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/button"
- android:id="@+id/button"/>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/imageView"/>
- </LinearLayout>
5、定义字符常量
strings.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <stringname="app_name">网络图片查看器</string>
- <stringname="imagepath">网络图片路径</string>
- <stringname="button">查看</string>
- <stringname="error">获取图片失败</string>
- </resources>
6、在AndroidManifest.xml文件里加入访问网络的权限,因为这里涉及到上网流量,会向用户收取一定的费用,在默认情况下是没有这个权限的,需要在此文件中申请权限,如下图:
好了,tomcat跑起来后在浏览器中能看到图片后,就把这个应用部署到模拟器上了。
部署完成后,就到了如下的页面:
点击上图的 查看 按钮,即可查看网络中的图片
二:查看页面源代码的功能和这个差不多,如下图:
下面显示源代码的部分的TextView需用ScrollView包裹起来,
- <ScrollView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/codeView"/>
- </ScrollView>
更多相关文章
- android之调用webservice 实现图片上传
- android 按钮设计中state_selected属性
- 图片缩放设置
- Android利用Fiddler进行网络数据抓包
- android 实现图片上传功能 Tomcat作为服务器
- Android(安卓): 一键分享(纲领)
- ListActivity中android:id="@+id/android:list"是啥玩意儿?
- Android之使用Android-query框架进行开发(一)
- Android-Module:ImageView常用XML属性