推荐安卓开发神器(里面有各种UI特效和android代码库实例)

网络图片查看器

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

[html] view plain copy
  1. |<!--访问internet权限-->
  2. <uses-permissionandroid:name="android.permission.INTERNET"/>

界面如下:

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

示例:

[html] view plain copy
  1. <spanstyle="FONT-WEIGHT:normal">publicclassMainActivityextendsActivity{
  2. privateEditTextimagepath;
  3. privateImageViewimageView;
  4. @Override
  5. publicvoidonCreate(BundlesavedInstanceState){
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.main);
  8. imagepath=(EditText)this.findViewById(R.id.imagepath);
  9. imageView=(ImageView)this.findViewById(R.id.imageView);
  10. Buttonbutton=(Button)this.findViewById(R.id.button);
  11. button.setOnClickListener(newView.OnClickListener(){
  12. publicvoidonClick(Viewv){
  13. Stringpath=imagepath.getText().toString();
  14. try{
  15. byte[]data=ImageService.getImage(path);//获取图片数据
  16. if(data!=null){
  17. //构建位图对象
  18. Bitmapbitmap=BitmapFactory.decodeByteArray(data,0,data.length);
  19. imageView.setImageBitmap(bitmap);//显示图片
  20. }else{
  21. Toast.makeText(getApplicationContext(),R.string.error,1).show();
  22. }
  23. }catch(Exceptione){
  24. Toast.makeText(getApplicationContext(),R.string.error,1).show();
  25. }
  26. }
  27. });
  28. }
  29. }</span>
[html] view plain copy
  1. <spanstyle="FONT-WEIGHT:normal">publicclassImageService{
  2. /**
  3. *获取图片
  4. *@parampath网络图片路径
  5. *@return图片的字节数据
  6. */
  7. publicstaticbyte[]getImage(Stringpath)throwsException{
  8. URLurl=newURL(path);
  9. HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
  10. //设置超时时间
  11. conn.setConnectTimeout(5000);
  12. conn.setRequestMethod("GET");
  13. if(conn.getResponseCode()==200){
  14. InputStreaminStream=conn.getInputStream();
  15. byte[]data=StreamTool.read(inStream);
  16. returndata;
  17. }
  18. returnnull;
  19. }
  20. }</span>
[html] view plain copy
  1. <spanstyle="FONT-WEIGHT:normal">publicclassStreamTool{
  2. /**
  3. *读取输入流数据
  4. *@paraminStream
  5. *@return
  6. */
  7. publicstaticbyte[]read(InputStreaminStream)throwsException{
  8. ByteArrayOutputStreamoutStream=newByteArrayOutputStream();
  9. byte[]buffer=newbyte[1024];
  10. intlen=0;
  11. while((len=inStream.read(buffer))!=-1){
  12. outStream.write(buffer,0,len);
  13. }
  14. inStream.close();
  15. returnoutStream.toByteArray();
  16. }
  17. }</span>

网页源码查看器

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

[html] view plain copy
  1. <ScrollView
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. >
  5. <TextView
  6. android:layout_width="fill_parent"
  7. android:layout_height="wrap_content"
  8. android:id="@+id/htmlsource"
  9. />
  10. </ScrollView>

界面如下:

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

示例

[html] view plain copy
  1. @Override
  2. publicvoidonCreate(BundlesavedInstanceState){
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.main);
  5. pathText=(EditText)this.findViewById(R.id.path);
  6. htmlsource=(TextView)this.findViewById(R.id.htmlsource);
  7. Buttonbutton=(Button)this.findViewById(R.id.button);
  8. button.setOnClickListener(newView.OnClickListener(){
  9. publicvoidonClick(Viewv){
  10. Stringpath=pathText.getText().toString();
  11. try{
  12. //获取源码
  13. Stringhtml=PageService.getHtml(path);
  14. htmlsource.setText(html);
  15. }catch(Exceptione){
  16. Toast.makeText(getApplicationContext(),R.string.error,1).show();
  17. }
  18. }
  19. });
  20. }

[html] view plain copy
  1. publicclassPageService{
  2. /**
  3. *获取网页源代码
  4. *@parampath网页路径
  5. *@return
  6. */
  7. publicstaticStringgetHtml(Stringpath)throwsException{
  8. URLurl=newURL(path);
  9. HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
  10. conn.setConnectTimeout(5000);
  11. conn.setRequestMethod("GET");
  12. if(conn.getResponseCode()==200){
  13. byte[]data=StreamTool.read(conn.getInputStream());
  14. returnnewString(data,"UTF-8");
  15. }
  16. returnnull;
  17. }
  18. }

更多相关文章

  1. Android 九宫格图片展示的实现
  2. 在Android中使用GIF图片
  3. Android 2.1 源码结构分析 转载
  4. Android植物大战僵尸附源码
  5. Android Telephony —— 手机信号实时变化源码分析过程记录
  6. android 开发之电子钢琴 源码

随机推荐

  1. Android(安卓)ListView使用
  2. Android(安卓)图片加载缓存
  3. Android(安卓)之 资源自适应与国际化
  4. Android(安卓)存储用户信息
  5. Google Android(安卓)文档笔记-Training-
  6. android CoordinatorLayout 使用
  7. Android(安卓)Action Bar
  8. android 版本号比较大小
  9. Android(安卓)旋转图片
  10. iphone、android的mimetype