• 从网络获取图片示例


    本示例使用于4.0一下的Android系统

    1、编写activity_main.xml文件

    <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"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <ImageView
            android:id="@+id/iv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1000" />
    
        <EditText
            android:id="@+id/et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="http://g.hiphotos.baidu.com/image/w%3D2048/sign=94bc5b60a50f4bfb8cd0995437777af0/86d6277f9e2f07083e861effe824b899a801f249.jpg" />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="commit" />
    
    </LinearLayout>
    View Code

    2、编写MainActivity.java文件

    package com.example.picturelook;
    
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import org.apache.http.HttpConnection;
    
    import android.os.Bundle;
    import android.R.integer;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.text.TextUtils;
    import android.view.Menu;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        private ImageView iView;
        private EditText editText;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            iView=(ImageView)findViewById(R.id.iv);
            editText=(EditText)findViewById(R.id.et);
        }
        public void click(View view)
        {
            String path=editText.getText().toString().trim();
            if (TextUtils.isEmpty(path)) {
                Toast.makeText(this, "图片路径不能为空", 0).show();
            }else {
                try {
                    //创建uRL链接
                    URL url=new URL(path);
                    //根据url发送Http请求
                    HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                    //设置请求方式/请求属性/读取超时时间/连接超时时间
                    conn.setRequestMethod("GET");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
                    conn.setReadTimeout(3000);
                    conn.setConnectTimeout(5000);
                    
                    //得到请求响应码
                    int code=conn.getResponseCode();
                    if (code==200) {
                        //获得图片输入流
                        InputStream inputStream=conn.getInputStream();
                        //将输入流转换成bitmap对象
                        Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
                        iView.setImageBitmap(bitmap);
                        }else {
                        Toast.makeText(this, "图片请求失败", 0).show();
                    }
                    
                    
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(this, "图片显示失败", 0).show();
                }
            }
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    }
    View Code

    3、添加权限

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.picturelook"
        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.example.picturelook.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>
    View Code

    demo下载

    以下是适配安装4.0及以上系统的代码

    package com.example.picturelook;
    
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.text.TextUtils;
    import android.view.Menu;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        private ImageView iView;
        private EditText editText;
        
        @SuppressLint("HandlerLeak")
        private Handler handler=new Handler(){
            public void handleMessage(Message msg) {
                if (msg.what==1) {
                    Bitmap bitmap=(Bitmap) msg.obj;
                    iView.setImageBitmap(bitmap);
                }
            };
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            iView=(ImageView)findViewById(R.id.iv);
            editText=(EditText)findViewById(R.id.et);
        }
        public void click(View view)
        {
            final String path=editText.getText().toString().trim();
            new Thread(new Runnable() {
                
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    if (TextUtils.isEmpty(path)) {
                        Toast.makeText(MainActivity.this, "图片路径不能为空", 0).show();
                    }else {
                        try {
                            //创建uRL链接
                            URL url=new URL(path);
                            //根据url发送Http请求
                            HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                            //设置请求方式/请求属性/读取超时时间/连接超时时间
                            conn.setRequestMethod("GET");
                            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
                            conn.setReadTimeout(3000);
                            conn.setConnectTimeout(5000);
                            
                            //得到请求响应码
                            int code=conn.getResponseCode();
                            if (code==200) {
                                //获得图片输入流
                                InputStream inputStream=conn.getInputStream();
                                //将输入流转换成bitmap对象
                                Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
                                Message msg=new Message();
                                msg.what=1;
                                msg.obj=bitmap;
                                handler.sendMessage(msg);
                                }else {
                                Toast.makeText(MainActivity.this, "图片请求失败", 0).show();
                            }
                            
                            
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            Toast.makeText(MainActivity.this, "图片显示失败", 0).show();
                        }
                    }
                }
            }).start();
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    }
    View Code
  • 相关阅读:
    映射dll到r0的高维注入
    winrar 去广告的姿势
    利用Hook的方式监控powershell的命令行参数
    读取QQ ClientKey失败分析
    Java——集合——泛型——泛型的概念&使用泛型的好处
    Java——集合——Collection集合——Iterator接口介绍&迭代器的代码实现&迭代器的实现原理&增强for循环
    Java——包装类详解
    Java——集合——泛型——定义和使用含有泛型的方法
    Java——Object类详解
    Java——集合——泛型——定义和使用含有泛型的类
  • 原文地址:https://www.cnblogs.com/hyzhou/p/3480031.html
Copyright © 2020-2023  润新知