Android查看网络图片例子

1.布局文件


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <EditText 
            android:id="@+id/et_url"
            android:layout_height="wrap_content"
            android:layout_width="0dip"
            android:text="http://img0.bdstatic.com/img/image/shouye/dengni37.jpg"
            android:singleLine="true"
            android:layout_weight="1"/>
        <Button 
            android:id="@+id/btn_submit"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Go"/>
    </LinearLayout>

</LinearLayout>

2.主程序中实现

package com.wzw.netphoto;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private ImageView ivIcon;
	private EditText etUrl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ivIcon = (ImageView) findViewById(R.id.iv_icon);
		etUrl = (EditText) findViewById(R.id.et_url);
		Button btn=(Button) findViewById(R.id.btn_submit);
		btn.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		String url=etUrl.getText().toString();
		
		Bitmap bit=getImageFromNet(url);
		ivIcon.setImageBitmap(bit);
		
	}
	
	public Bitmap getImageFromNet(String url){
		try {
			
			URL mUrl=new URL(url);//创建URL对象
			HttpURLConnection conn = (HttpURLConnection) mUrl.openConnection();
			conn.setRequestMethod("GET");
			
			conn.setConnectTimeout(10000);  //设置连接超时时间
			conn.setReadTimeout(5000);		//设置读取超时时间
			conn.connect();
			int responseCode = conn.getResponseCode();
			if(responseCode==200){
				InputStream is = conn.getInputStream();
				Bitmap bitmap=BitmapFactory.decodeStream(is);
				Toast.makeText(this, "获取成功", 0).show();
				return bitmap;
			}	
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return null;
	}

}

3.添加网络访问权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wzw.netphoto"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />、
      <!--   添加访问网络的权限 -->
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.wzw.netphoto.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>

</manifest>


Android查看网络图片例子,布布扣,bubuko.com

Android查看网络图片例子

上一篇:构建基于Javascript的移动CMS——生成博客(三).重构


下一篇:iOS_12_tableViewCell的删除更新_红楼梦