android从网络获取图片以及异步加载图片
2012-06-27 10:46
691 查看
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Environment; import android.util.Log; import android.widget.ImageView; /** * 处理图片的工具类 */ public class ImageUtils { /** * 图片基准路径 */ private static final String BASE_SDCARD_IMAGES = "/mnt/sdcard/demo/images/"; private static final String TAG = "ImageUtils"; /** * 判断文件是否存在 * * @param 文件在本地的完整名 * @return */ private static boolean judgeExists(String fullName) { File file = new File(fullName); return file.exists(); } /** * 获取最后的‘/’后的文件名 * * @param name * @return */ private static String getLastName(String name) { int lastIndexOf = 0; try { lastIndexOf = name.lastIndexOf('/'); } catch (Exception e) { e.printStackTrace(); } return !name.equals("") ? name.substring(lastIndexOf + 1) : ""; } /** * 拼接一个完整的本地文件名 * @param 文件的网络路径 * @return */ private static String getImageFullName(String name) { return BASE_SDCARD_IMAGES + getLastName(name); } /** * 通过该网络路径获取Bitmap * @param 该图片的网络路径 */ public static Bitmap getBitmap(String urlPath) { Bitmap bitmap = null; String fullName = getImageFullName(urlPath); if (ImageUtils.judgeExists(fullName)) { /* 存在就直接使用 */ Log.e(TAG, "使用了sdcard里的图片"); bitmap = BitmapFactory.decodeFile(fullName); } else { /* 去下载图片,下载完成之后返回该对象 */ Log.e(TAG, "去下载了图片"); bitmap = downloadAndSaveBitmap(urlPath, fullName); } return bitmap; } /** * 下载保存图片 * * @param urlPath * 下载路径 * @param fullName * 文件保存路径+文件名 * @return */ private static Bitmap downloadAndSaveBitmap(String urlPath, String fullName) { Bitmap bitmap = downloadImage(urlPath); /* 首先判断是否挂载了sdcard */ if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { if (bitmap != null) { saveBitmap(fullName, bitmap); } } else { Log.e(TAG, "没有sdcard无法保存图片"); } return bitmap; } /** * 保存图片 * * @param fullName * @param bitmap */ private static void saveBitmap(String fullName, Bitmap bitmap) { if (bitmap != null) { try { File file = new File(fullName); if (!file.exists()) { creatFolder(fullName); file.createNewFile(); } FileOutputStream fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); } catch (IOException e) { e.printStackTrace(); Log.e(TAG, "图片保存失败,异常信息是:" + e.toString()); } } else { Log.e(TAG, "没有下载成功图片,无法保存"); } } /** * 创建保存文件的文件夹 * * @param fullName * 带文件名的文件路径 * @return */ private static void creatFolder(String fullName) { if (getLastName(fullName).contains(".")) { String newFilePath = fullName.substring(0, fullName .lastIndexOf('/')); File file = new File(newFilePath); file.mkdirs(); } } /** * 下载图片 * * @param urlPath * @return */ private static Bitmap downloadImage(String urlPath) { try { byte[] byteData = getImageByte(urlPath); if (byteData == null) { Log.e(TAG, "没有得到图片的byte,问题可能是path:" + urlPath); return null; } int len = byteData.length; BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; options.inPurgeable = true; options.inInputShareable = true; options.inJustDecodeBounds = false; if (len > 200000) {// 大于200K的进行压缩处理 options.inSampleSize = 2; } return BitmapFactory.decodeByteArray(byteData, 0, len); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, "图片下载失败,异常信息是:" + e.toString()); return null; } } /** * 获取图片的byte数组 * * @param urlPath * @return */ private static byte[] getImageByte(String urlPath) { InputStream in = null; byte[] result = null; try { URL url = new URL(urlPath); HttpURLConnection httpURLconnection = (HttpURLConnection) url .openConnection(); httpURLconnection.setDoInput(true); httpURLconnection.connect(); if (httpURLconnection.getResponseCode() == 200) { in = httpURLconnection.getInputStream(); result = readInputStream(in); in.close(); } else { Log .e(TAG, "下载图片失败,状态码是:" + httpURLconnection.getResponseCode()); } } catch (Exception e) { Log.e(TAG, "下载图片失败,原因是:" + e.toString()); e.printStackTrace(); } finally { Log.e(TAG, "下载图片失败!"); if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } /** * 将输入流转为byte数组 * * @param in * @return * @throws Exception */ private static byte[] readInputStream(InputStream in) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = in.read(buffer)) != -1) { baos.write(buffer, 0, len); } baos.close(); in.close(); return baos.toByteArray(); } /** * 此方法用来异步加载图片 * @param imageview * @param path */ public static void downloadAsyncTask(final ImageView imageview, final String path) { new AsyncTask<String, Void, Bitmap>() { @Override protected Bitmap doInBackground(String... params) { return getBitmap(path); } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); if (result != null && imageview != null) { imageview.setImageBitmap(result); } else { Log.e(TAG, "在downloadAsyncTask里异步加载图片失败!"); } } }.execute(new String[] {}); } }
相关文章推荐
- android从网络获取图片以及异步加载图片
- android从网络获取图片以及异步加载图片
- android工具类----(从网络获取图片以及异步加载图片)
- Android 解决ListView异步加载网络数据(图片文字)出现位置错乱以及优化ListView的加载
- Android异步加载获取网络数据(图片)
- Android 网络通信框架Volley简介以及获取JSON对象和图片加载例子
- Android异步加载网络图片精华版
- Android异步加载网络图片
- 在Android的应用中实现网络图片异步加载的方法
- 在android中 软引用SoftReference异步加载网络图片
- Android ListView异步加载网络图片出现位置错乱闪烁及优化ListView的加载
- 【Android】网络图片加载优化(一)利用弱引用缓存异步加载
- Android异步加载网络图片-good
- ListView优化,获取网络图片异步加载,分批加载,分页显示,图片缓存等优化方式
- 网络图片异步加载类库--Android-Universal-Image-Loader的使用
- Android实现异步从网络加载图片列表和上拉加载更多、下拉刷新列表(使用xListView框架实现)
- Android ListView异步加载图片错位、重复、闪烁分析以及解决方案
- Android异步加载网络图片
- Android 异步加载网络图片并缓存到本地
- Android异步任务AsyncTask使用解析 获取网络图片