如何在 Android 当中显示网络图片
2010-04-21 01:35:00 来源:WEB开发网42. myFileUrl = new URL(url);
43. } catch (MalformedURLException e) {
44. e.printStackTrace();
45. }
46. try {
47. HttpURLConnection conn = (HttpURLConnection) myFileUrl
48. .openConnection();
49. conn.setDoInput(true);
50. conn.connect();
51. InputStream is = conn.getInputStream();
52. bitmap = BitmapFactory.decodeStream(is);
53. is.close();
54. } catch (IOException e) {
55. e.printStackTrace();
56. }
57. return bitmap;
58. }
59.
60. }
package com.jinyan.image; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; 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.ImageView; public class ActivityMain extends Activity { /** Called when the activity is first created. */ String imageUrl = "http://i.pbase.com/o6/92/229792/1/80199697.uAs58yHk.50pxCross_of_the_Knights_Templar_svg.png"; Bitmap bmImg; ImageView imView; Button button1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imView = (ImageView) findViewById(R.id.imview); imView.setImageBitmap(returnBitMap(imageUrl)); } public Bitmap returnBitMap(String url) { URL myFileUrl = null; Bitmap bitmap = null; try { myFileUrl = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); } try { HttpURLConnection conn = (HttpURLConnection) myFileUrl .openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); is.close();
更多精彩
赞助商链接