您的位置:首页 > 理论基础 > 计算机网络

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 把 把值设置进去就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐