android获取网络图片的用法
来源:互联网 发布:python 约瑟夫环 编辑:程序博客网 时间:2024/05/16 13:55
关于android获取网络图片主要是把网络图片的数据流读入到内存中然后用
1.Bitmap bitMap = BitmapFactory.decodeByteArray(data, 0, length);
方法来将图片流传化为bitmap类型 这样才能用到
1.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字节数组并不是想象中的数组!而是把输入流传化为字节内存输出流的字节数组格式
1.Bitmap bitMap = BitmapFactory.decodeByteArray(data, 0, length);
方法来将图片流传化为bitmap类型 这样才能用到
1.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 {
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()方法的代码清单
下面是改进后的 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();
}
- android获取网络图片的用法
- android获取网络图片的用法
- android获取网络图片的用法 BitmapFactory.decodeByteArray
- android获取网络图片的用法 BitmapFactory.decodeByteArray 返回null
- android - 获取网络图片
- android获取网络图片
- android获取网络图片
- Android获取网络图片
- android 获取网络图片
- android 获取网络图片
- android 获取网络图片
- Android获取网络图片
- Android 获取网络图片
- android 获取网络图片
- android 获取网络图片
- android获取网络图片
- android获取网络上的图片
- Android获取网络图片的方法
- MySQL存储过程
- MySQL存储过程
- Android调用天气预报的WebService简单例子
- .NET事务处理
- Android OpenGL ES 开发教程(17):投影变换Projection
- android获取网络图片的用法
- Android应用自动更新功能的实现
- Android测试教程(17):Monkey 命令行工具
- xAd:南京大学大二学生开发的视频内广告动态植入技术
- 黑链-最高明的网站隐藏链接方法
- Android ApiDemos示例解析(12):App->Activity->Redirection
- 用链接标签触发onclick事件而不跳转
- 关于线程的句柄资源
- apache 多端口设置