欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程顯示網(wǎng)絡(luò)上的圖片實(shí)例詳解

 更新時(shí)間:2016年10月28日 12:15:38   作者:pku_android  
這篇文章主要介紹了Android編程顯示網(wǎng)絡(luò)上的圖片,結(jié)合實(shí)例形式詳細(xì)分析了Android顯示網(wǎng)絡(luò)圖片的流程與具體操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程顯示網(wǎng)絡(luò)上的圖片的方法。分享給大家供大家參考,具體如下:

在Android中顯示網(wǎng)絡(luò)上的圖片,需要先根據(jù)url找到圖片地址,然后把該圖片轉(zhuǎn)化成Java的InputStream,然后把該InputStream流轉(zhuǎn)化成BitMap,BitMap可以直接顯示在android中的ImageView里。這就是顯示網(wǎng)絡(luò)上圖片的思路,實(shí)現(xiàn)起來(lái)很簡(jiǎn)單。下面讓我們看一下實(shí)現(xiàn)起來(lái)的過(guò)程。

首先在AndroidManifest.xml中給程序加上訪(fǎng)問(wèn)Internet的權(quán)限:

復(fù)制代碼 代碼如下:
<uses-permissionandroid:name="android.permission.INTERNET" />

然后在布局文件中加入一個(gè)ImageView,用來(lái)顯示網(wǎng)絡(luò)上的圖片:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView" />
</LinearLayout>

在主程序的Activity中寫(xiě)從網(wǎng)絡(luò)中得到圖片,并轉(zhuǎn)化成InputStream,然后再轉(zhuǎn)化成可以顯示在ImageView里的Bitmap。

package com.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.widget.ImageView;
public class NetImageActivity extends Activity {
  /** Called when the activity is first created. */
   String imageUrl = "http://content.52pk.com/files/100623/2230_102437_1_lit.jpg";
   Bitmap bmImg;
   ImageView imView;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    imView = (ImageView) findViewById(R.id.imageView);
    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;
  }
}

然后運(yùn)行程序就可以顯示出來(lái)網(wǎng)絡(luò)上的圖片了。

運(yùn)行效果:

PS:關(guān)于AndroidManifest.xml權(quán)限控制詳細(xì)內(nèi)容可參考本站在線(xiàn)工具:

Android Manifest功能與權(quán)限描述大全:
http://tools.jb51.net/table/AndroidManifest

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android圖形與圖像處理技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論