<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="请输入图片网址"></TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/editText_url"></EditText>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="获取" android:id="@+id/button_get"></Button>
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_gravity="center"
android:id="@+id/p_w_picpathView"></ImageView>
</LinearLayout>
import com.yanjun.getImage.GetImage;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
/**
* 通过网络获取图片
* @author YanJun
*<uses-permission android:name="android.permission.INTERNET"></uses-permission>
*/
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
EditText p_w_picpathEditText = null ;
Button button ;
ImageView p_w_picpathView ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
p_w_picpathEditText = (EditText) findViewById(R.id.editText_url);
p_w_picpathView = (ImageView) findViewById(R.id.p_w_picpathView);
button = (Button) findViewById(R.id.button_get);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String Url = p_w_picpathEditText.getText().toString();
try {
//得到图片二进制数据
byte[] data = GetImage.getImage(Url);
//将二进制数据转换成位图
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//显示图片
p_w_picpathView.setImageBitmap(bitmap);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "链接延时", Toast.LENGTH_LONG).show();
Log.e(TAG, e.toString());
}
}
});
}
}
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class GetImage {
/**
* 获取路径方法
* @param path 路径
* @return
* @throws Exception
*/
public static byte[]getImage(String path) throws Exception {
// 从网络上获取图片--UUL对象用来封装路径
URL url = new URL(path);
// 打开路径链接---得到HttpURLConnection对象
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// 通过HTTP协议请求网络图片---设置请求方式:get/post
httpURLConnection.setRequestMethod("GET");
// 设置连接超时
httpURLConnection.setConnectTimeout(5*1000);
// 从外界想手机应用内传递数据----通过输入流获取图片数据
InputStream inputStream = httpURLConnection.getInputStream();
// 从输入流中获取图片的二进制数据----readInputStream()
return StreamTool.readInputStream(inputStream);
}
}
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class StreamTool {
/**从输入流获取数据
* @param inSream 输入流
* @return
* @throws Exception
*/
public static byte[] readInputStream(InputStream inSream) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 定义一个缓冲区
byte[] buffer = new byte[1024];
int len = 0;
// 不断的从流里读取数据---while循环---nSream.read(buffer)表示从流里读取数据到缓冲区
// 读取到末尾时,返回值是-1;
while ((len = inSream.read(buffer)) != -1) {
// 将缓冲区的数据写到输出流中
byteArrayOutputStream.write(buffer, 0, len);
}
inSream.close();
return byteArrayOutputStream.toByteArray();
}
}