zoukankan      html  css  js  c++  java
  • 读取网络图片

    利用bitmap读取网络图片,太简单没什么好说的,注意更新UI要在主线程上,不然会报错。

    package com.example.web_bitmap;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import com.example.web_bitmap.R.id;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
        private ImageView show;
        private static final String PATH = "http://b.hiphotos.baidu.com/image/pic/item/08f790529822720efdd99bf379cb0a46f21faba0.jpg";
        private Handler handle=new Handler(){
            public void handleMessage(Message msg) {
                Bitmap bit=(Bitmap) msg.obj;
                show.setImageBitmap(bit);
            }
            
        };
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            show = (ImageView) findViewById(id.show);
            Thread thread = new Thread(null, webInback, "readImage");
            thread.start();
        }
    
        public Runnable webInback = new Runnable() {
            public void run() {
                byte data[]=readBitmap();
                Bitmap bit=BitmapFactory.decodeByteArray(data, 0, data.length);
                Message msg=Message.obtain();
                msg.obj=bit;
                handle.sendMessage(msg);
            }
        };
    
        public byte[] readBitmap() {
            // 内存操作流
            ByteArrayOutputStream bos = null;
            try {
                URL url = new URL(PATH);
                bos = new ByteArrayOutputStream();
                byte data[] = new byte[1024];
                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();
                InputStream inputStream = urlConnection.getInputStream();
                int length = 0;
                while ((length = inputStream.read(data)) != -1) {
                    bos.write(data, 0, length);
                }
            } catch (Exception e) {
                System.out.println("读取失败");
            } finally {
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return bos.toByteArray() ;
            
        }
    
        
        
        
        
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }

     

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        tools:context="com.example.web_bitmap.MainActivity" >
    
        <ImageView
            android:id="@+id/show"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="@string/web" />
    
    </LinearLayout>

     

  • 相关阅读:
    C++ STL 一般总结(转载)
    Python高级语法总结
    Adaboost 算法的原理与推导——转载及修改完善
    简化版SMO算法标注
    【转载】机器学习算法基础概念学习总结
    C++中添加配置文件读写方法
    Python中scatter()函数--转载
    python 之 strip()--(转载)
    zabbix邮件报警脚本(Python)
    Linux常用命令
  • 原文地址:https://www.cnblogs.com/mydomainlistentome/p/4703213.html
Copyright © 2011-2022 走看看