关于android获取网络图片主要是吧网络图片的数据流读入到内存中然后用
Bitmap bitMap = BitmapFactory.decodeByteArray(data, 0, length);

方法来将图片流传化为bitmap类型 这样才能用到
imageView.setImageBitmap(bitMap);

来进行转化,这些大家都了解,主要的问题在获取bitmap时候居然为null 怎么回事呢?我开始的时候遇见这个问题,并且困扰了一段时间,最后发现错误了
下面是我的错误代码
     byte[] data = GetImageForNet.getImage(path);     int length = data.length;         Bitmap bitMap = BitmapFactory.decodeByteArray(data, 0, length);     imageView.setImageBitmap(bitMap);

下面是 GetImageForNet.getImage()方法的代码清单
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;byte[] b = new byte[1024];int len = -1;if (httpURLconnection.getResponseCode() == 200) { in = httpURLconnection.getInputStream(); in.read(b);                         in.close();                         return b;                          }return null;}

看起来没有问题 获取网络图片输入流,填充二进制数组,返回二进制数组,然后使用 Bitmap bitMap = BitmapFactory.decodeByteArray(data, 0, length); data就是返回的二进制数组
获取bitMap 看起来没有问题,可是bitMap就是为null!汗!最后慢慢整理思路,排除法去查找出现异常的地方,最后发现可能BitmapFactory.decodeByteArray方法中所需要的data不一定是传统意义上的字节数组,查看android api,最后发现BitmapFactory.decodeByteArray所需要的data字节数组并不是想象中的数组!而是把输入流传化为字节内存输出流的字节数组格式,找到原因后改进一下,成功!附上成功后的代码
  try {    byte[] data = GetImageForNet.getImage(path);    String d = new String(data);   // File file = new File("1.jpg");    //OutputStream out = new FileOutputStream(file);    //out.write(data);    //out.close();    int length = data.length;    Bitmap bitMap = BitmapFactory.decodeByteArray(data, 0, length);    imageView.setImageBitmap(bitMap);    //imageView.seti} catch (Exception e) {Log.i(TAG, e.toString());Toast.makeText(DataActivity.this, "获取图片失败", 1).show();}

下面是改进后的 GetImageForNet.getImage()方法的代码清单
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;byte[] b = new byte[1024];int len = -1;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();}
转自:http://www.4ucode.com/Study/Topic/1952873

更多相关文章

  1. android关于View的截图
  2. Android(安卓)CountDownTimer
  3. Android编程心得-在任意类中获取当前屏幕宽高
  4. Android(安卓)蓝牙开启关闭
  5. Android:图片不加载到内存获取图片的大小
  6. Android(安卓)ACCESS_COARSE_LOCATION的用途和介绍
  7. Android(安卓)进阶——代码插桩必知必会之初识ASM7字节码操作库
  8. Android(安卓)获取OnItemClick事件中组件的内容
  9. GBD调试谷歌拼音输入法准备工作

随机推荐

  1. Android优酷播放器SDK——Eclipse工程迁
  2. windows系统上安装与使用Android NDK r5
  3. Android使用Toast显示消息提示框
  4. Android应用开发学习笔记之播放音频
  5. android 退出应用程序
  6. Android实用问题汇总
  7. Android 开发学习 HelloAndroid例子
  8. Android读写XML(下)——创建XML文档
  9. Android入门教程 AsyncTask的使用及execu
  10. 如何编译MTK android模拟器