国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2019-10-22 18:29:52
字體:
來源:轉載
供稿:網友

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

看下實現效果圖:

android從網絡中獲得一張圖片并顯示在屏幕上,Android,獲取網絡圖片

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>  

注意訪問網絡中的數據需要添加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();  //通過輸入流獲得圖片數據     byte[] data = StreamTool.readInputStream(inputStream);   //獲得圖片的二進制數據     return data;        } } 

4:StreamTool的內容

package com.capinfotech.utils;  import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream;  public class StreamTool {    /*    * 從數據流中獲得數據    */   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 獲取網路圖片并顯示的實例,如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 塔河县| 酒泉市| 本溪市| 靖江市| 宜川县| 绍兴市| 丰县| 凤台县| 阳泉市| 岱山县| 利津县| 宝鸡市| 皋兰县| 赤峰市| 中宁县| 宁波市| 陆丰市| 湘潭县| 鄂尔多斯市| 崇明县| 开封市| 麦盖提县| 兴宁市| 响水县| 十堰市| 龙海市| 延边| 延长县| 余姚市| 香格里拉县| 盱眙县| 师宗县| 连平县| 渝中区| 大石桥市| 镇坪县| 肃宁县| 建昌县| 犍为县| 东港市| 泌阳县|