关闭

Android开发 通过httpURL获取图片

149人阅读 评论(0) 收藏 举报

MainActivity

创建imageservice业务类,以及getImage方法

package com.example.p4;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.service.ImageService;

public class MyActivity extends Activity {
    private EditText text;
    private ImageView imageView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text = (EditText) this.findViewById(R.id.text);
        imageView = (ImageView) this.findViewById(R.id.image);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new ButtonClickListener());
        /*因为Android4.0之后主线程不允许http请求,所以必须增加以下两行代码*/
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

    }

    private final class ButtonClickListener implements View.OnClickListener {
        public void onClick(View v) {
            String path = text.getText().toString();
            try
            {
                byte[] data = ImageService.getImage(path);//位图存储图片
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//二进制位图显示
                imageView.setImageBitmap(bitmap);//显示图片
            }
            catch (Exception e)
            {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(),R.string.toast,Toast.LENGTH_SHORT).show();
            }

        }

    }
}



mageservice业务类


package com.service;

import com.utils.StreamTool;

import javax.net.ssl.HttpsURLConnection;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by 宝冬 on 2015/7/8.
 */
public class ImageService
{

/*
* 获取网络图片
* 输入path
* 返回位图
* */
    public static byte[] getImage(String path)throws Exception
    {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();//得到基于http协议的连接对象
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");//请求方式
        if (conn.getResponseCode()==200)//通过状态码确定是否请求成功,成功时的状态码为200
        {
            InputStream inStream = conn.getInputStream();//得到输入流
            return StreamTool.read(inStream);//在流工具类中直接调用读取方法
        }
        return null;
    }
}


创建StreamTool类,比较常用,只需要创建后向其中增添方法

package com.utils;

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


public class StreamTool {
    /*
    * 读取流中的数据
    * */
    public static byte[] read(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream =new ByteArrayOutputStream();
        byte[] buffer = new byte[9999];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1)//读取流中的数据
        {
            outStream.write(buffer,0,len);
        }
        inStream.close();
        return outStream.toByteArray();
    }
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3272次
    • 积分:224
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档