Android 怎么用网上的图片设置ImageView背景?
Android怎么用网上的图片设置ImageView的背景?如下这种方法不知道为什么运行不出来,求高手解答URLpicUrl=null;try{picUrl=newURL...
Android 怎么用网上的图片设置ImageView的背景?如下这种方法不知道为什么运行不出来,求高手解答URL picUrl = null;
try {picUrl = new URL("http://www.xxx.com/xxx.jpg");}
catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();}
Bitmap pngBM = null;
try {pngBM = BitmapFactory.decodeStream(picUrl.openStream());}
catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} imageView.setImageBitmap(pngBM); 展开
try {picUrl = new URL("http://www.xxx.com/xxx.jpg");}
catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();}
Bitmap pngBM = null;
try {pngBM = BitmapFactory.decodeStream(picUrl.openStream());}
catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} imageView.setImageBitmap(pngBM); 展开
2个回答
2015-12-08 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
Android 用网上的图片设置ImageView背景可以使用如下方法:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myimage);
ImageView image1 = (ImageView) findViewById(R.myImage.image);
//Bitmap bitmap = getLoacalBitmap(“/aa/aa.jpg”); //从本地取图片
Bitmap bitmap = getHttpBitmap(“http://blog.3gstdy.com/wp-content/themes/twentyten/images/headers/path.jpg”); //从网上取图片
image1 .setImageBitmap(bitmap); //设置Bitmap
}
/**
* 加载本地图片
* http://bbs.3gstdy.com
* @param url
* @return
*/
public static Bitmap getLoacalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* 从服务器取图片
*http://bbs.3gstdy.com
* @param url
* @return
*/
public static Bitmap getHttpBitmap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
Log.d(TAG, url);
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setConnectTimeout(0);
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myimage);
ImageView image1 = (ImageView) findViewById(R.myImage.image);
//Bitmap bitmap = getLoacalBitmap(“/aa/aa.jpg”); //从本地取图片
Bitmap bitmap = getHttpBitmap(“http://blog.3gstdy.com/wp-content/themes/twentyten/images/headers/path.jpg”); //从网上取图片
image1 .setImageBitmap(bitmap); //设置Bitmap
}
/**
* 加载本地图片
* http://bbs.3gstdy.com
* @param url
* @return
*/
public static Bitmap getLoacalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* 从服务器取图片
*http://bbs.3gstdy.com
* @param url
* @return
*/
public static Bitmap getHttpBitmap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
Log.d(TAG, url);
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setConnectTimeout(0);
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询