栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android编程实现网络图片查看器和网页源码查看器实例

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android编程实现网络图片查看器和网页源码查看器实例

本文实例讲述了Android编程实现网络图片查看器和网页源码查看器。分享给大家供大家参考,具体如下:

网络图片查看器

清单文加入网络访问权限:




界面如下:

 

示例:

public class MainActivity extends Activity {
  private EditText imagepath;
  private ImageView imageView;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    imagepath = (EditText) this.findViewById(R.id.imagepath);
    imageView = (ImageView) this.findViewById(R.id.imageView);
    Button button = (Button) this.findViewById(R.id.button);
    button.setonClickListener(new View.onClickListener() {
      public void onClick(View v) {
 String path = imagepath.getText().toString();
 try{
   byte[] data = ImageService.getImage(path);//获取图片数据
   if(data!=null){
     //构建位图对象
     Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
     imageView.setImageBitmap(bitmap);//显示图片
   }else{
     Toast.makeText(getApplicationContext(), R.string.error, 1).show();
   }
 }catch (Exception e) {
   Toast.makeText(getApplicationContext(), R.string.error, 1).show();
 }
      }
    });
  }
}

public class ImageService {
  
  public static byte[] getImage(String path) throws Exception{
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    //设置超时时间
    conn.setConnectTimeout(5000);
    conn.setRequestMethod("GET");
    if(conn.getResponseCode()==200){
      InputStream inStream = conn.getInputStream();
      byte[] data = StreamTool.read(inStream);
      return data;
    }
    return null;
  }
}

public class StreamTool {
  
  public static byte[] read(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();
  }
}

网页源码查看器

如果网页的源码超过屏幕的显示位置的话,要求出现滚动条.





界面如下:

 

示例

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   pathText = (EditText) this.findViewById(R.id.path);
   htmlsource = (TextView) this.findViewById(R.id.htmlsource);
   Button button = (Button) this.findViewById(R.id.button);
   button.setonClickListener(new View.onClickListener() {
  public void onClick(View v) {
    String path = pathText.getText().toString();
    try{
      //获取源码
      String html = PageService.getHtml(path);
      htmlsource.setText(html);
    }catch (Exception e) {
      Toast.makeText(getApplicationContext(), R.string.error, 1).show();
    }
  }
});
}

public class PageService {
  
  public static String getHtml(String path) throws Exception{
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(5000);
    conn.setRequestMethod("GET");
    if(conn.getResponseCode() == 200){
      byte[] data = StreamTool.read(conn.getInputStream());
      return new String(data, "UTF-8");
    }
    return null;
  }
}

希望本文所述对大家Android程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/163673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号