android 基础 网络获取网页的源文件和图片
2012-09-25 15:50
453 查看
先打开数据连接,
public static byte[] getImage(String path) throws IOException { URL url = new URL("http://news.xinhuanet.com/mil/2012-09/11/123697757_11n.jpg"); HttpURLConnection connection = (HttpURLConnection) url.openConnection();//获取数据连接 connection.setRequestMethod("GET");//请求连接的方法 connection.setConnectTimeout(5000);//请求超时 InputStream inputStream = connection.getInputStream();// 获取请求连接返回的数据流 byte[] data = StreamTool.readInputStream(inputStream);// 读取流数据,得到图片的二进制数据 return data; }
读取流的方法
public static byte[] readInputStream(InputStream inputStream) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } inputStream.close(); return outputStream.toByteArray(); }
界面的控制
package com.itcast.net_img; import java.io.IOException; import com.itcast.service.ImageService; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; /** * 涉及到网络图片 所以要加网络访问权限 * * @author w * */ public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private EditText editText; private ImageView imageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.imgPath); imageView = (ImageView) findViewById(R.id.imgView); Button button = (Button) findViewById(R.id.imgButton); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String imgpath = editText.toString(); try { byte[] data = ImageService.getImage(imgpath); Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// 生成位图 imageView.setImageBitmap(bitmap);// 显示位图 } catch (IOException e) { e.printStackTrace(); Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show(); Log.i(TAG, e.toString()); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/img_label" /> <EditText android:id="@+id/imgPath" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://news.xinhuanet.com/mil/2012-09/11/123697757_11n.jpg" /> <Button android:id="@+id/imgButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/img_button" /> <ImageView android:id="@+id/imgView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
上面是显示图片,若果是返回网页的源码,让读取的二进制流转换成字符型,在声明个textview 把 把值设置进去就可以了
相关文章推荐
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例
- 【android初级】之Android之从网络上获取数据(图片,网页,xml,json等)
- 【android基础】之Android获取网络上的图片结合ImageView的简单应用
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- android 从网络上获取数据(图片,网页,xml,json等)
- Android之网络操作 - 从网络获取图片或网页
- Android之网络操作 - 从网络获取图片或网页
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android基础-获取网络上的图片并显示
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- android网络编程之——客户端获取网页的图片
- Android之网络操作 - 从网络获取图片或网页
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- android开发--获取网络图片,读取进度条
- android 网络图片与网页读取
- Android中获取网络图片的三种方法