当前位置: 代码迷 >> Android >> Android兑现网络图片查看器和网页源码查看器
  详细解决方案

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

热度:79   发布时间:2016-05-01 19:53:38.0
Android实现网络图片查看器和网页源码查看器

?

网络图片查看器

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

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

?

界面如下:


示例:

[html]?view plaincopy
  1. <span?style="FONT-WEIGHT:?normal">public?class?MainActivity?extends?Activity?{??
  2. ????private?EditText?imagepath;??
  3. ????private?ImageView?imageView;??
  4. [email protected]
  5. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  6. ????????super.onCreate(savedInstanceState);??
  7. ????????setContentView(R.layout.main);??
  8. ??????????
  9. ????????imagepath?=?(EditText)?this.findViewById(R.id.imagepath);??
  10. ????????imageView?=?(ImageView)?this.findViewById(R.id.imageView);??
  11. ??????????
  12. ????????Button?button?=?(Button)?this.findViewById(R.id.button);??
  13. ????????button.setOnClickListener(new?View.OnClickListener()?{????????????
  14. ????????????public?void?onClick(View?v)?{??
  15. ????????????????String?path?=?imagepath.getText().toString();??
  16. ????????????????try{??
  17. ????????????????????byte[]?data?=?ImageService.getImage(path);//获取图片数据??
  18. ????????????????????if(data!=null){??
  19. ????????????????????????//构建位图对象??
  20. ????????????????????????Bitmap?bitmap?=?BitmapFactory.decodeByteArray(data,?0,?data.length);??
  21. ????????????????????????imageView.setImageBitmap(bitmap);//显示图片??
  22. ????????????????????}else{??
  23. ????????????????????????Toast.makeText(getApplicationContext(),?R.string.error,?1).show();??
  24. ????????????????????}?????????????????????
  25. ????????????????}catch?(Exception?e)?{??
  26. ????????????????????Toast.makeText(getApplicationContext(),?R.string.error,?1).show();??
  27. ????????????????}??
  28. ????????????}??
  29. ????????});??
  30. ????}??
  31. }</span>??
[html]?view plaincopy
  1. <span?style="FONT-WEIGHT:?normal">public?class?ImageService?{??
  2. ????/**??
  3. ?????*?获取图片??
  4. [email protected]
  5. [email protected]
  6. ?????*/??
  7. ????public?static?byte[]?getImage(String?path)?throws?Exception{??
  8. ????????URL?url?=?new?URL(path);??
  9. ????????HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection();??
  10. ????????//设置超时时间??
  11. ????????conn.setConnectTimeout(5000);??
  12. ????????conn.setRequestMethod("GET");??
  13. ????????if(conn.getResponseCode()==200){??
  14. ????????????InputStream?inStream?=?conn.getInputStream();??
  15. ????????????byte[]?data?=?StreamTool.read(inStream);??
  16. ????????????return?data;??
  17. ????????}??
  18. ????????return?null;??
  19. ????}??
  20. }</span>??
[html]?view plaincopy
  1. <span?style="FONT-WEIGHT:?normal">public?class?StreamTool?{??
  2. ????/**??
  3. ?????*?读取输入流数据??
  4. [email protected]
  5. [email protected]
  6. ?????*/??
  7. ????public?static?byte[]?read(InputStream?inStream)?throws?Exception{??
  8. ????????ByteArrayOutputStream?outStream?=?new?ByteArrayOutputStream();??
  9. ????????byte[]?buffer?=?new?byte[1024];??
  10. ????????int?len?=?0;??
  11. ????????while(?(len?=?inStream.read(buffer))?!=?-1?){??
  12. ????????????outStream.write(buffer,?0,?len);??
  13. ????????}??
  14. ????????inStream.close();??
  15. ????????return?outStream.toByteArray();??
  16. ????}??
  17. }</span>??

?


网页源码查看器


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

?

[html]?view plaincopy
  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>??

?

?

?

?

?

?

?

?

?

?

?

界面如下:

示例

?

[html]?view plaincopy
  1. [email protected]
  2. ??public?void?onCreate(Bundle?savedInstanceState)?{??
  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. ????????
  8. ??????Button?button?=?(Button)?this.findViewById(R.id.button);??
  9. ??????button.setOnClickListener(new?View.OnClickListener()?{??
  10. ??????
  11. ????public?void?onClick(View?v)?{??
  12. ????????String?path?=?pathText.getText().toString();??
  13. ????????try{??
  14. ????????????//获取源码??
  15. ????????????String?html?=?PageService.getHtml(path);??
  16. ????????????htmlsource.setText(html);??
  17. ????????}catch?(Exception?e)?{??
  18. ????????????Toast.makeText(getApplicationContext(),?R.string.error,?1).show();??
  19. ????????}??
  20. ????}??
  21. });??
  22. ??}??

?

[html]?view plaincopy
  1. public?class?PageService?{??
  2. ????/**??
  3. ?????*?获取网页源代码??
  4. [email protected]
  5. [email protected]
  6. ?????*/??
  7. ????public?static?String?getHtml(String?path)?throws?Exception{??
  8. ????????URL?url?=?new?URL(path);??
  9. ????????HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection();??
  10. ????????conn.setConnectTimeout(5000);??
  11. ????????conn.setRequestMethod("GET");??
  12. ????????if(conn.getResponseCode()?==?200){??
  13. ????????????byte[]?data?=?StreamTool.read(conn.getInputStream());??
  14. ????????????return?new?String(data,?"UTF-8");??
  15. ????????}??
  16. ????????return?null;??
  17. ????}??
  18. }??

?

  相关解决方案