Android通过网络URL获取图片并显示
2017-03-18 17:10
543 查看
1.布局文件
<uses-permission android:name="android.permission.INTERNET" />
3.主要代码
下载地址:Android通过网络URL获取图片并显示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/button" android:layout_width="300dp" android:layout_height="70dp" android:layout_gravity="center_horizontal" android:text="获取网络图片"/> </LinearLayout>2.在AndroidManifest.xml添加可以访问网络的权限
<uses-permission android:name="android.permission.INTERNET" />
3.主要代码
package com.example.pic; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private Button btn; private ImageView imageview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) this.findViewById(R.id.button); imageview = (ImageView) this.findViewById(R.id.imageView); StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build()); StrictMode.setVmPolicy( new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build()); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { btn.setEnabled(false); String strURL = "http://192.168.31.184:8011/main.png"; try { Bitmap bitmap = getBitmap(strURL); imageview.setImageBitmap(bitmap); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } public Bitmap getBitmap(String path) throws IOException { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if (conn.getResponseCode() == 200) { InputStream inputStream = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); return bitmap; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }下面这一段代码很重要
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build()); StrictMode.setVmPolicy( new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
下载地址:Android通过网络URL获取图片并显示
相关文章推荐
- Android 通过url获取网络图片并显示在ImageView中
- android开发-通过HttpURLConnection获取网络内容,Bitmap把二进制格式的图片转为位图
- android:通过url向网络端获取json格式数据并解析显示
- android 获取网络图片显示在Gallery中
- 使用android-rss库实现从网络中获取rss信息,并通过ListView显示
- 【Android】给定URL显示网络图片的实现方法
- Android ListView从网络获取图片及文字显示(转)
- Android通过URL获取网络数据
- Android ListView从网络获取图片及文字显示
- (转)Android通过URL获取网络数据
- 获取服务器Url图片资源,并显示在ImageView中 Android
- android GridView 通过json显示网络图片和文字
- Android ListView从网络获取图片及文字显示
- Android通过URL获取网络资源
- Android使用HttpURLConnection显示网络图片
- C# 通过URL获取图片并显示在PictureBox上的方法
- ios通过URL地址,从网络上获取图片
- android HttpURLConnection编程详解,获取网络图片,html代码查
- C# 通过URL获取图片并显示在PictureBox上的方法
- Android通过url获取bitmap图片问题