• 读取网络图片


    利用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>

     

  • 相关阅读:
    第一篇
    面试题
    CSS样式
    html初步学习
    web开发项目连接访问数据库
    oracle数据库操作之连接
    oracle数据库的基本操作(create创建表,update更新表,drop删除表,select查询表,insert插入数据)
    用js在前台界面进行账户密码的检测,账户和密码符合要求后可进行登录
    用servlet代替js对登录进行检测
    JSP转发和重定向的区别,以及如何获取数据
  • 原文地址:https://www.cnblogs.com/mydomainlistentome/p/4703213.html
Copyright © 2020-2023  润新知