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

Android從網絡中獲得一張圖片并顯示在屏幕上的實例詳解

 更新時間:2017年08月18日 11:32:00   投稿:lqh  
這篇文章主要介紹了Android從網絡中獲得一張圖片并顯示在屏幕上的實例詳解的相關資料,希望通過本文能幫助大家實現(xiàn)這樣的功能,需要的朋友可以參考下

Android從網絡中獲得一張圖片并顯示在屏幕上的實例詳解

看下實現(xiàn)效果圖:

1:androidmanifest.xml的內容

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
   package="cn.capinftotech.image" 
   android:versionCode="1" 
   android:versionName="1.0"> 
  <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".MainActivity" 
         android:label="@string/app_name"> 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
        <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
    </activity> 
 
  </application> 
  <uses-sdk android:minSdkVersion="8" /> 
  <uses-permission android:name="android.permission.INTERNET" /> 
 
</manifest>  

注意訪問網絡中的數(shù)據需要添加android.permission.INTERNET權限

2:MainActivity的內容

package cn.capinftotech.image; 
 
import java.io.IOException; 
 
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.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.Toast; 
 
import com.capinfotech.service.ImageService; 
 
public class MainActivity extends Activity { 
  private static final String TAG = "MainActivity"; 
   
  private EditText urlPath = null; 
  private Button button = null; 
  private ImageView imageView = null; 
   
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
     
    urlPath = (EditText)findViewById(R.id.urlpath); 
    button = (Button)findViewById(R.id.button); 
    imageView = (ImageView)findViewById(R.id.imageView); 
     
    button.setOnClickListener(new View.OnClickListener() { 
       
      @Override 
      public void onClick(View v) { 
        String urlPathContent = urlPath.getText().toString(); 
        try { 
          byte[] data = ImageService.getImage(urlPathContent); 
          Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); //生成位圖 
          imageView.setImageBitmap(bitmap);  //顯示圖片 
        } catch (IOException e) { 
          Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_LONG).show(); //通知用戶連接超時信息 
          Log.i(TAG, e.toString()); 
        } 
         
      } 
    }); 
  } 
} 

3:ImageService類的內容

package com.capinfotech.service; 
 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
 
import com.capinfotech.utils.StreamTool; 
 
public class ImageService { 
   
  public static byte[] getImage(String path) throws IOException { 
    URL url = new URL(path); 
    HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 
    conn.setRequestMethod("GET");  //設置請求方法為GET 
    conn.setReadTimeout(5*1000);  //設置請求過時時間為5秒 
    InputStream inputStream = conn.getInputStream();  //通過輸入流獲得圖片數(shù)據 
    byte[] data = StreamTool.readInputStream(inputStream);   //獲得圖片的二進制數(shù)據 
    return data; 
     
  } 
} 

4:StreamTool的內容

package com.capinfotech.utils; 
 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
 
public class StreamTool { 
 
  /* 
   * 從數(shù)據流中獲得數(shù)據 
   */ 
  public static byte[] readInputStream(InputStream inputStream) throws IOException { 
    byte[] buffer = new byte[1024]; 
    int len = 0; 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    while((len = inputStream.read(buffer)) != -1) { 
      bos.write(buffer, 0, len); 
    } 
    bos.close(); 
    return bos.toByteArray(); 
     
  } 
} 

5:程序中用到的字符串資源strings.xml里的內容

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
  <string name="hello">Hello World, MainActivity!</string> 
  <string name="app_name">圖片瀏覽器</string> 
  <string name="urlpath">網絡圖片地址</string> 
  <string name="button">顯示</string> 
  <string name="error">網絡連接超時</string> 
</resources> 

6:程序布局文件main.xml的內容

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  > 
<TextView  
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"  
  android:text="@string/urlpath" 
  /> 
<EditText  
  android:id="@+id/urlpath" 
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"  
  android:text="http://www.eoeandroid.com/data/attachment/forum/201107/18/142935bbi8d3zpf3d0dd7z.jpg" 
  /> 
<Button 
  android:id="@+id/button" 
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:text="@string/button" 
  /> 
<ImageView 
  android:id="@+id/imageView" 
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
/> 
</LinearLayout> 

以上使用Android 獲取網路圖片并顯示的實例,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

  • Android ViewFlipper翻轉視圖使用詳解

    Android ViewFlipper翻轉視圖使用詳解

    這篇文章主要為大家詳細介紹了Android ViewFlipper翻轉視圖的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 自定義Android注解系列教程之注解變量

    自定義Android注解系列教程之注解變量

    這篇文章主要給大家介紹了關于自定義Android注解系列教程之注解變量的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-07-07
  • Android實現(xiàn)在map上畫出路線的方法

    Android實現(xiàn)在map上畫出路線的方法

    這篇文章主要介紹了Android實現(xiàn)在map上畫出路線的方法,較為詳細的分析了Android在map上繪制路線所涉及的map圖調用、畫筆的使用、頁面布局及權限控制的相關技巧,需要的朋友可以參考下
    2015-07-07
  • Android自定義View實現(xiàn)抖音飄動紅心效果

    Android自定義View實現(xiàn)抖音飄動紅心效果

    這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)抖音飄動紅心效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Android OkHttp Post上傳文件并且攜帶參數(shù)實例詳解

    Android OkHttp Post上傳文件并且攜帶參數(shù)實例詳解

    這篇文章主要介紹了Android OkHttp Post上傳文件并且攜帶參數(shù)實例詳解的相關資料,需要的朋友可以參考下
    2017-03-03
  • Android中wifi與數(shù)據流量的切換監(jiān)聽詳解

    Android中wifi與數(shù)據流量的切換監(jiān)聽詳解

    本文主要介紹了Android中wifi與數(shù)據流量的切換監(jiān)聽的方法步驟。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • Android基礎知識之tween動畫效果

    Android基礎知識之tween動畫效果

    Android基礎知識之tween動畫效果,Android一共提供了兩種動畫,這篇文章主要介紹了Android動畫效果之tween動畫,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android Fragment多層嵌套重影問題的解決方法

    Android Fragment多層嵌套重影問題的解決方法

    這篇文章主要介紹了Android Fragment多層嵌套重影問題的解決方法,從解決bug的思想,導致原因,原理解析等方面找出問題所在原因,最終解決方法就可以簡單了,對fragment 多層嵌套問題感興趣的朋友一起通過本文學習吧
    2016-08-08
  • Android checkbox的listView(多選,全選,反選)具體實現(xiàn)方法

    Android checkbox的listView(多選,全選,反選)具體實現(xiàn)方法

    由于listview的一些特性,剛開始寫這種需求的功能的時候都會碰到一些問題,重點就是存儲每個checkbox的狀態(tài)值,在這里分享出了完美解決方法:
    2013-06-06
  • Android 消息分發(fā)使用EventBus的實例詳解

    Android 消息分發(fā)使用EventBus的實例詳解

    這篇文章主要介紹了Android 消息分發(fā)使用EventBus的實例詳解的相關資料,在項目中用了許多Handler和broadcast導致代碼冗余,顯得雜亂無章,這里使用EventBus來實現(xiàn)相同的功能,需要的朋友可以參考下
    2017-07-07

最新評論