返回顶部

收藏

Android 通过网络获取图片的代码

更多

主activity

package com.netimg;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
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 AndroidnetimgActivity extends Activity {
    /** Called when the activity is first created. */
    //定义所使用的组件
    private Button button;
    private EditText editText;
    private ImageView imagesView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //实例化用到的组件
        editText =(EditText)findViewById(R.id.EditText);
        imagesView = (ImageView)findViewById(R.id.ImageView);
        button = (Button)findViewById(R.id.Button);

        //为按钮添加监听事件
        button.setOnClickListener(new buttonListener());

    }

    private final class buttonListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            String path = editText.getText().toString();

            System.out.println(path);

            //通过业务类ImageService的getImage方法得到数据
            try {
                byte[] data = ImageService.getImage(path);
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                imagesView.setImageBitmap(bitmap);//显示图片
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "获取失败", 1).show();
            }

        }

    }
}

                                业务类
package com.netimg;

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

public class ImageService {

    public static byte[] getImage(String path) throws Exception {

        URL url = new URL(path);
        //基于HTTP协议连接对象
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();

        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");

        if(conn.getResponseCode() == 200){
            InputStream inStream = conn.getInputStream();
            return StreamTool.read(inStream);
        }

        return null;
    }

}

                                工具类
package com.netimg;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StreamTool {

    public static byte[] read(InputStream inStream) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len = inStream.read(buffer)) != -1){
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();
    }

}

                                获取网络权限
<uses-permission android:name="android.permission.INTERNET"/>

标签:获取图片,android

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2014-06-30 06:35:44 一个android sqlite CRUD代码生成小工具
  2. xiaoli.wang 发表 2017-06-27 12:59:16 Wine 2.11开发版发布
  3. 博主 发表 2015-01-19 00:00:00 Android开发中遇到的那些坑
  4. 博主 发表 2015-10-24 14:44:02 解决adb启动失败问题
  5. zrenx 发表 2010-05-10 08:06:34 Debian 调试 Nexus One 的设置
  6. Vansfans_Neo 发表 2017-07-04 09:58:18 Vans 潮流壁纸 Vol.170704
  7. 博主 发表 2015-02-02 08:23:32 [Android] wrong playback state when playing MIDI a
  8. moxun 发表 2016-07-28 12:51:40 Android局域网通信机制初探
  9. 博主 发表 2013-08-19 07:00:00 about android boot.img
  10. songhua.gao 发表 2017-07-13 10:48:18 IOS开发入门你们准备好了吗?
  11. Fromnowon 发表 2012-11-26 17:27:23 安卓4.0添加农历锁屏(12.4,代码修复)
  12. Vansfans_Sherley 发表 2017-07-19 15:01:10 Vans 潮流壁纸 Vol.170719

发表评论