安卓网页html/图片查看器开发——张泽华老师的些许错误
2014-06-04 10:13
513 查看
1.在Activity中开发界面,一个url 输入框、一个确定查看按钮、一个ImageView或者TextView。
html的界面Activity代码
我看的是黑马张泽华老师的安卓视频,张泽华老师在很多方面表现出了真正的才气。不过人无完人,张泽华老师在提到Html编码的问题上,出现了些许错误...
编码处理可以使用
html的界面Activity代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> <EditText android:id="@+id/et_input_uri" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/et_uri_hint" android:lines="2" android:text="http://10.0.2.2:8080/bbsModelV_11270826/pages/index" /> <Button android:id="@+id/bt_get_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bt_getimage" /> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv_show_img" android:layout_width="fill_parent" android:layout_height="fill_parent" android:contentDescription="@string/iv_cntdesc" android:scaleType="center" /> </ScrollView> </LinearLayout>2.编写Activity对应的代码
package com.example.netimgviewer; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import org.apache.http.util.ByteArrayBuffer; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private EditText et_url; private Button btn_view; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_url = (EditText) findViewById(R.id.et_input_uri); btn_view = (Button) findViewById(R.id.bt_get_img); tv = (TextView) findViewById(R.id.tv_show_img); // 注册监听 btn_view.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_get_img: String imgurl = et_url.getText().toString().trim(); Toast.makeText(this, imgurl, Toast.LENGTH_LONG).show(); try { tv.setText(getHtml(imgurl)); /** * iv..setImageBitmap(this.getbitmap(imgurl)) */ } catch (Exception e) { // TODO Auto-generated catch block Toast.makeText(this, imgurl + "地址链接不上!", Toast.LENGTH_LONG) .show(); e.printStackTrace(); } break; default: break; } } public Bitmap getbitmap(String address) throws Exception { // 声明 URL URL url = new URL(address); // 得到打开的链接 URLConnection conn = url.openConnection(); conn.setRequestProperty("method", "get"); conn.setReadTimeout(5000); InputStream is = conn.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int len = 0; while ((len = is.read(bytes)) != -1) { bos.write(bytes, 0, len); } byte[] imgBytes = bos.toByteArray(); return BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length); } public String getHtml(String address) throws Exception { // 声明 URL URL url = new URL(address); // 得到打开的链接 URLConnection conn = url.openConnection(); conn.setRequestProperty("method", "get"); conn.setReadTimeout(5000); InputStream is = conn.getInputStream(); // 得到网页编码类型 String contentType = conn.getContentType(); // 截取等号 = 得到结果如:utf-8、GBK String type = contentType.split("=")[1]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int len = 0; while ((len = is.read(bytes)) != -1) { bos.write(bytes, 0, len); } byte[] imgBytes = bos.toByteArray(); return new String(imgBytes, type) + "======" + type; } }总结:
我看的是黑马张泽华老师的安卓视频,张泽华老师在很多方面表现出了真正的才气。不过人无完人,张泽华老师在提到Html编码的问题上,出现了些许错误...
编码处理可以使用
String contentType = conn.getContentType(); // 截取等号 = 得到结果如:utf-8、GBK String type = contentType.split("=")[1]; return new String(imgBytes, type) + "======" + type;
相关文章推荐
- 网页设计中HTML常范的五个错误
- 开始追求完美--网页设计中HTML常范的五个错误
- 网页图片显示错误,显示默认图片
- HTML to Image in C#指定网页地址,获取内容为图片
- asp.net正则表达式提取网页网址、标题、图片实例以及过滤所有HTML标签实例
- WebService开发时错误,Received content of invalid Content-Type setting:text/html - SOAP expects "text/xml"
- 关于开发高手2004第8期中,<jsp创建基于WEB的动态图表>一文中的些许错误
- JS网页图片查看器-可控制图片放大缩小还原移动效果
- 网页标题显示为图片的HTML代码
- JS网页图片查看器-可控制图片放大缩小移动|兼容IE、FF
- 网页开发中的容易忽略的问题 javascript HTML中的table
- 网页设计中HTML常范的五个错误
- 网页制作html-给光标一个跟随的图片
- Javascript 网页开发—体验式学习教程(1) html 和 CSS
- JS网页图片查看器-可控制图片放大缩小还原移动效果
- 网页设计中HTML常范的五个错误
- 通过url地址抓取网页html代码 - 可视化Java快速开发平台 - CSDNBlog
- 强制html网页中某个图片刷新
- JS网页图片查看器-可控制图片放大缩小还原移动效果
- asp.net正则表达式提取网页网址、标题、图片实例以及过滤所有HTML标签实例