Android显示网络图片相关实现方法浅谈

 

Android显示网络图片Step1:
//浏览网络图片
public class WebImageViewActivity extends Activity implements OnItemClickListener
{
 //String imageUrl = "http://i.pbase.com/o6/92/229792/1/80199697.uAs58yHk.50pxCross_of_the_Knights_Templar_svg.png"; 
 public static String imageUrl = "";
   Bitmap bmImg; 
   ImageView imView;
  
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.web_imageview_activity);
  
  //imageUrl="http://www.iwapzone.com/templets/default/images/apc_bg.png";
  imView = (ImageView) findViewById(R.id.imgview); 

  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();

  } catch (IOException e) {
   e.printStackTrace();
  }

  return bitmap;

 }


 @Override
 protected void onDestroy()
 {
  super.onDestroy();
  Intent intent = new Intent(this, PlayFileService.class);
  stopService(intent);
 }

 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  // TODO Auto-generated method stub
  
 }

}

其中,returnBitMap(String url) 方法就是具体实现网络图片转换成bitmap。

 

Android显示网络图片Step2:

web_imageview_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
      android:layout_width="fill_parent" android:layout_height="fill_parent"
      android:orientation="vertical" android:background="@android:color/transparent"   >
  <ImageView  
 android:id="@+id/imgview"
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"  
 android:layout_gravity="center"   />

</LinearLayout>

 

Android显示网络图片Step3:

在你的AndroidManifest.xml文件的< /manifest>节点上面添加

< uses-permission android:name="Android.permission.INTERNET" />,这是由于Android有很多的权限限制,否则图片是不能在你的模拟器上显示的。

 

 

http://www.189works.com/article-102567-1.html

 

http://www.189works.com/article-2612-1.html

 

http://wenku.baidu.com/view/1ecca51c964bcf84b9d57b80.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值