创建Bitmap之BitmapFactory

package com.loaderman.customviewdemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

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

public class MainActivity extends Activity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView iv = (ImageView) findViewById(R.id.img);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    byte[] data = getImage("https://files-cdn.cnblogs.com/files/loaderman/lp.bmp");
                    int length = data.length;

                    final Bitmap bitMap = BitmapFactory.decodeByteArray(data, 0, length);

                    iv.post(new Runnable() {
                        @Override
                        public void run() {
                            iv.setImageBitmap(bitMap);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

    public static byte[] getImage(String path) throws Exception {
        URL url = new URL(path);
        HttpURLConnection httpURLconnection = (HttpURLConnection) url.openConnection();
        httpURLconnection.setRequestMethod("GET");
        httpURLconnection.setReadTimeout(6 * 1000);
        InputStream in = null;
        if (httpURLconnection.getResponseCode() == 200) {
            in = httpURLconnection.getInputStream();
            byte[] result = readStream(in);
            in.close();
            return result;

        }
        return null;
    }

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

}
package com.loaderman.customviewdemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

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

public class MainActivity extends Activity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView iv = (ImageView) findViewById(R.id.img);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    InputStream inputStream = getImage("https://files-cdn.cnblogs.com/files/loaderman/lp.bmp");
                    final Bitmap bitMap = BitmapFactory.decodeStream(inputStream);

                    iv.post(new Runnable() {
                        @Override
                        public void run() {
                            iv.setImageBitmap(bitMap);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

    public static InputStream getImage(String path) throws Exception {
        URL url = new URL(path);
        HttpURLConnection httpURLconnection = (HttpURLConnection) url.openConnection();
        httpURLconnection.setRequestMethod("GET");
        httpURLconnection.setReadTimeout(6 * 1000);
        if (httpURLconnection.getResponseCode() == 200) {
            return httpURLconnection.getInputStream();

        }
        return null;
    }


}

BitmapFactory用于各种资源,文件,数据流和字节数组中创建bitmap位图对象,BitmapFactory是一个工具类,,提供大量的函数,可以解析和创建bitmap对象

  public static Bitmap decodeFile(String pathName, BitmapFactory.Options opts) {
        throw new RuntimeException("Stub!");
    }

    public static Bitmap decodeFile(String pathName) {//通过文件路径来加载图片,必须是全路径名
        throw new RuntimeException("Stub!");
    }

    public static Bitmap decodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts) {
        throw new RuntimeException("Stub!");
    }

    public static Bitmap decodeResource(Resources res, int id, BitmapFactory.Options opts) {
        throw new RuntimeException("Stub!");
    }

    public static Bitmap decodeResource(Resources res, int id) {//表示从资源中解码一张位图,res一般通过,id为资源id
        throw new RuntimeException("Stub!");
    }

    public static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts) {
        throw new RuntimeException("Stub!");
    }

    public static Bitmap decodeByteArray(byte[] data, int offset, int length) {//根据byte数组解析出bitmap
        throw new RuntimeException("Stub!");
    }

    public static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts) {
        throw new RuntimeException("Stub!");
    }

    public static Bitmap decodeStream(InputStream is) {//一般用于加载网络上获取的图片
        throw new RuntimeException("Stub!");
    }

    public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts) {
        throw new RuntimeException("Stub!");
    }

    public static Bitmap decodeFileDescriptor(FileDescriptor fd) {//fd包含解码位图数据的文件路径 比 decodeFile更节省内存
        throw new RuntimeException("Stub!");
    }

 

转载于:https://www.cnblogs.com/loaderman/p/10231410.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值