android平台根据自己输入的地址下载网络上的图片实现方法
2012-12-25 19:12:12 来源:WEB开发网核心提示:import android.os.Bundle;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.util.Log;import android
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageview=(ImageView) findViewById(R.id.imageView1);
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String path="http://list.image.baidu.com/t/image/w_bizhi.jpg";
try {
byte[] data=ImageService.getImage(path);
Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);
imageview.setImageBitmap(bm);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), "出错啦", 1);
Log.e("ERR",e.toString());
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
第三步:
单独写一个imageservice类
package com.hsbi.csdn.service;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class ImageService {
public static byte[] getImage(String path) throws Exception{
URL url=new URL(path);//得到代表路径的url;
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(20*1000);
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageview=(ImageView) findViewById(R.id.imageView1);
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String path="http://list.image.baidu.com/t/image/w_bizhi.jpg";
try {
byte[] data=ImageService.getImage(path);
Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);
imageview.setImageBitmap(bm);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), "出错啦", 1);
Log.e("ERR",e.toString());
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
第三步:
单独写一个imageservice类
package com.hsbi.csdn.service;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class ImageService {
public static byte[] getImage(String path) throws Exception{
URL url=new URL(path);//得到代表路径的url;
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(20*1000);
更多精彩
赞助商链接