android 网络图片路径解析和本地图片路径解析的方法 ,base64加密报文解析成bitmap【代码示例】

因为项目的需要,需要做一个拍照、相册获取图片,并且上传图片到网络上面的做法。其中,用到了

一、把本地图片路径解析显示到ImageView上;

1、

Bitmap bitmap = null;
bitmap = getLoacalBitmap(url);
imageView.setImageBitmap(bitmap);

/**
* 加载本地图片
* from Internet
* @param url
* @return
*/
public static Bitmap getLoacalBitmap(String url) {
    try {
          FileInputStream fis = new FileInputStream(url);
          return BitmapFactory.decodeStream(fis);
    } catch (FileNotFoundException e) {
          e.printStackTrace();
          returnnull;
    }
}


 此方法可以用下面的方法替换: 

2、

Bitmap bitmap = null;
bitmap = BitmapFactory.decodeFile(url);
imageView.setImageBitmap(bitmap);




二、把图片网络路径解析显示到ImageView上;

方法1、

Bitmap bitmap = null;
new Thread(getHttpBitmapRunnable).start();

Runnable  getHttpBitmapRunnable =new Runnable(){
	@Override
	public void run() {
       	byte[] data =null;
		try {
			data = getImage(logo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Message msg =new Message();
		msg.what = 0;
		msg.obj = bitmap;
		getLogoHandler.sendMessage(msg);
	}
};
Handler getLogoHandler = new Handler(){
	public void handleMessage(android.os.Message msg) {
		if(msg.what == 0){
			iv_userphoto.setImageBitmap((Bitmap)(msg.obj));  
		}
	}
};
public staticbyte[] getImage(String urlPath) throws Exception{
<span style="white-space:pre">	</span>URL url = new URL(urlPath);
<span style="white-space:pre">	</span>HttpURLConnection conn = (HttpURLConnection)url.openConnection();
<span style="white-space:pre">	</span>conn.setRequestMethod("GET");
<span style="white-space:pre">	</span>conn.setConnectTimeout(5*1000);
<span style="white-space:pre">	</span>InputStream inStream = conn.getInputStream();
<span style="white-space:pre">	</span>return readFromInput(inStream);
}
public staticbyte[] readFromInput(InputStream inStream)throws Exception{
<span style="white-space:pre">	</span>ByteArrayOutputStream outStream = new ByteArrayOutputStream();
<span style="white-space:pre">	</span>byte[] buffer = new byte[1024];
<span style="white-space:pre">	</span>int len = 0;
<span style="white-space:pre">	</span>while((len = inStream.read(buffer))!= -1){
<span style="white-space:pre">		</span>outStream.write(buffer,0,len);
<span style="white-space:pre">	</span>}
inStream.close();
return outStream.toByteArray();
}


方法2、
new Thread(getHttpBitmapRunnable).start();
public Bitmap getHttpBitmap(String data)  {  
        Bitmap bitmap = null;  
        try  
        {  
            //初始化一个URL对象  
            URL url = new URL(data);  
            //获得HTTPConnection网络连接对象  
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
            connection.setConnectTimeout(5*1000);  //设置连接主机超时(单位:毫秒)
            connection.setReadTimeout(5*1000);//设置从主机读取数据超时(单位:毫秒)
            connection.setDoInput(true);  
            connection.connect();  
            //得到输入流  
            InputStream is = connection.getInputStream();  
            Log.i("TAG", "*********inputstream**"+is);  
            bitmap = BitmapFactory.decodeStream(is);           
            Log.i("TAG", "*********bitmap****"+bitmap);  
            //关闭输入流  
            is.close();  
            //关闭连接  
            connection.disconnect();  
        } catch (Exception e)  
        {             
            e.printStackTrace();  
        }    
        return bitmap;  
    }  
Runnable  getHttpBitmapRunnable = new Runnable(){
<span style="white-space:pre">	</span>@Override
<span style="white-space:pre">	</span>public void run() {
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>Bitmap bitmap = getHttpBitmap();
<span style="white-space:pre">		</span>}catch (Exception e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>} 
<span style="white-space:pre">		</span>Message msg = new Message();
<span style="white-space:pre">		</span>msg.what = 0;
<span style="white-space:pre">		</span>msg.obj =bitmap;
<span style="white-space:pre">		</span>getLogoHandler.sendMessage(msg);
<span style="white-space:pre">	</span>}
};
Handler getLogoHandler = new Handler(){
<span style="white-space:pre">	</span>public void handleMessage(android.os.Message msg) {
<span style="white-space:pre">		</span>if(msg.what == 0){
<span style="white-space:pre">			</span>imageView.setImageBitmap((Bitmap)(msg.obj));  
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
};



最后,附上从http请求接收到的base64报文解析成bitmap 的代码


String base64 = (String)objMap.get("base64");
imageView.setImageBitmap(stringtoBitmap(base64));


public Bitmap stringtoBitmap(String string) {
        // 将字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
                byte[] bitmapArray;
                bitmapArray = Base64.decode(string, Base64.DEFAULT);
                bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
                                bitmapArray.length);
        } catch (Exception e) {
                e.printStackTrace();
        }
        return bitmap;
}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值