返回顶部

收藏

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. Solo 发表 2014-12-11 05:10:00 Android,如何在代码中获取attr属性的值
  2. Vansfans_Sherley 发表 2017-11-08 13:18:12 Vans 潮流壁纸 Vol.171108
  3. 博主 发表 2015-03-21 14:15:28 【置顶】Android Design资源汇总
  4. 脉脉不得语 发表 2018-01-15 03:50:19 Android开发技术周报 Issue#163
  5. 梦之星辰-怀 发表 2015-01-28 03:37:21 HttpURLConnection之Get
  6. 博主 发表 2017-09-10 00:00:00 Android 中的任务和返回栈
  7. 博主 发表 2014-10-22 15:06:43 Android应用UI架构
  8. 脉脉不得语 发表 2017-11-06 03:53:23 Android开发技术周报 Issue#153
  9. 博主 发表 2014-03-22 17:12:49 Android Issue: readObject method throws ClassNotFo
  10. dqriot 发表 2018-01-24 11:02:12 Android平台挖矿木马研究报告
  11. 晓_晨DEV (cctanfujun@1 发表 2015-05-07 00:00:00 无线调试你的Android设备
  12. admin 发表 2018-01-25 18:00:00 Android Wear SDK and Emulator Update

发表评论