【android】简单的根据url下载图片的一个类
2012-06-28 17:59
549 查看
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;
public class DownloadFileUtil {
public static Bitmap downloadFile( String url) {
Bitmap bitmap = null;
try {
/*// //////////////取得的是byte数组, 从byte数组生成bitmap
byte[] data = getImage(url);
if (data != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
data.length);// bitmap
imageView.setImageBitmap(bitmap);// display image
} else {
// Toast.makeText(AndroidTest2_3_3.this, "Image error!",
// 1).show();
}*/
// ******** 取得的是InputStream,直接从InputStream生成bitmap ***********/
bitmap = BitmapFactory.decodeStream(getImageStream(url));
// ********************************************************************/
} catch (Exception e) {
//Toast.makeText(AndroidTest2_3_3.this, "Newwork error!", 1).show();
e.printStackTrace();
}
return bitmap;
}
private static byte[] getImage(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return readStream(inStream);
}
return null;
}
private static byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
private static InputStream getImageStream(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return conn.getInputStream();
}
return null;
}
}
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;
public class DownloadFileUtil {
public static Bitmap downloadFile( String url) {
Bitmap bitmap = null;
try {
/*// //////////////取得的是byte数组, 从byte数组生成bitmap
byte[] data = getImage(url);
if (data != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
data.length);// bitmap
imageView.setImageBitmap(bitmap);// display image
} else {
// Toast.makeText(AndroidTest2_3_3.this, "Image error!",
// 1).show();
}*/
// ******** 取得的是InputStream,直接从InputStream生成bitmap ***********/
bitmap = BitmapFactory.decodeStream(getImageStream(url));
// ********************************************************************/
} catch (Exception e) {
//Toast.makeText(AndroidTest2_3_3.this, "Newwork error!", 1).show();
e.printStackTrace();
}
return bitmap;
}
private static byte[] getImage(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return readStream(inStream);
}
return null;
}
private static byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
private static InputStream getImageStream(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return conn.getInputStream();
}
return null;
}
}
相关文章推荐
- 分享一个Spring MVC 根据图片URL,下载图片的方法
- Android中用URL模拟一个简单的图片加载器
- 根据URL下载图片至客户端、服务器的简单实例
- android根据url下载图片
- 根据一个url从网上下载图片
- Java和Android Http连接程序:使用java.net.URL 下载服务器图片到客户端
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- Android 使用URL下载图片到本地并显示
- HttpURLConnection根据URL下载图片
- Android中如何根据图片url路径来获取网络图片
- 一个简单的Android图片处理Demo
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- Java和Android Http连接程序:使用java.net.URL 下载服务器图片到客户端
- Android中如何根据图片url路径来获取网络图片
- 学习Android开源项目-根据知乎日报API分析重构一个简单的知乎日报Android客户端
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- 根据URL下载图片至客户端、服务器实例
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- android 图片下载的一个库 还有个http请求列子
- 做一个简单的Android图片缓存