码迷,mamicode.com
首页 > 其他好文 > 详细

读取网络图片

时间:2015-08-04 22:31:55      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:

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

 

读取网络图片

标签:

原文地址:http://www.cnblogs.com/mydomainlistentome/p/4703213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!