Android开发 通过httpURL获取图片

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();
    }
}



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AmarokAlonso/article/details/46813025
上一篇Android 开发 SQLite数据库
下一篇Android开发 通过URL获取网页源代码(滚动显示)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭