现在的位置: 首页 > 综合 > 正文

网上下载图片并保持到SD卡中+读取SD卡中的图片

2014年01月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 评论关闭

在这两个星期的项目开发中,东京的一个项目需要获取用户的头像并显示。先下载到本地,然后读取。每次登陆都要更新一次头像。为此,我封装了一个类,用于诸如此类的操作:ImageHelper.java

package net.emgz.android.provider.impl;

import java.io.BufferedOutputStream;
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;

/**
 * 在项目中经常会遇到保存网络上的图片或者其他文件到本地中,
 * 如SD卡。这个类能够很方便的实现网络上文件的下载和保存.这里是下载和保存网络上的图片
 * @author Kaven
 * @version 2012-07-16
 */
public class ImageHelper {
	public ImageHelper() {
		
	}

	/**
	 * @param url 要下载的文件地址
	 * @return Bitmap对象
	 */
	public static Bitmap DownloadImageFromNet(String url) {
		Bitmap bt = null;
		try {
			/*
			 * *************** 取得的是byte数组, 从byte数组生成bitmap***********************
			 * byte[] data =
			 * getImage(filePath); if(data!=null){ 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 ***********/
			bt = BitmapFactory.decodeStream(getImageStream(url));
			if (bt != null) {
				return bt;
			}
		} catch (Exception e) {
			// Toast.makeText(context, "Newwork error!",
			// Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 构建输入流
	 * 
	 * @param path
	 *            The path of image
	 * @return InputStream
	 * @throws Exception
	 */
	public 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;
	}

	/**
	 * 把下载的图片保存到本地,如果已经存在同名的文件,则覆盖。保存的类型是JPEG
	 * 
	 * @param bm Bitmap实例
	 * @param fileName
	 *            指定图片的名字
	 * @param savePath
	 *            要把图片保存到的路径
	 * @throws IOException
	 */
	public static void saveFile(Bitmap bm, String fileName, String savePath)
			throws IOException {
		if (bm != null) {
			String SAVE_PATH = savePath;
			File dirFile = new File(SAVE_PATH);
			if (!dirFile.exists()) {
				dirFile.mkdir();
			}
			File myCaptureFile = new File(SAVE_PATH + fileName);
			if (myCaptureFile != null) {  //如果已经存在同名的文件,则覆盖
				myCaptureFile.delete();
			}
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(myCaptureFile));
			bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
			bos.flush();
			bos.close();
		}
	}
	
	/**
	 * @param localPath 本地中文件的路径 如:"mmt/sdcard/userdata/image"  
	 * @return Bitmap
	 */
	public  static Bitmap getLocalImage(String localPath){
		Bitmap bm = BitmapFactory.decodeFile(localPath);
		return bm;
	}
	
}

应用的一个实例:因为考虑到下载的操作可能会耗时比较长,所以需要把下载的动作放到线程里面。

/**
	 * Load the user's image
	 * @author Su
	 * @version 2012.07.14
	 */
	public void downLoadUserImage() {
		
		final String uri = HttpService.BASE_HOST + HttpService.USER_AVATAR_URI;
		final URI expanded = new UriTemplate(uri).expand(userinfo.getUserCd(),
				HttpService.IMAGE_SIZE);
		final String download_uri = expanded.toString();

		new Thread() {

			@Override
			public void run() {
				bitmap = ImageHelper
						.DownloadImageFromNet(download_uri);
				Message msg = new Message();
				try {
					if (bitmap != null) {
						ImageHelper.saveFile(bitmap, HttpService.SAVE_NAME,
								HttpService.SAVE_PATH);
						msg.what = 0;
						handler.sendMessage(msg);
					} else {
						Bitmap bm = BitmapFactory
								.decodeFile(HttpService.LOAD_PATH);
						if (bm != null) {
							msg.what = 0;
							handler.sendMessage(msg);
						}
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}.start();
	}

下载后存到本地,然后从本地中显示刚刚下载的图片:

final ImageView image = (ImageView) findViewById(R.id.userinfo_avater);
Bitmap bm = BitmapFactory.decodeFile(HttpService.LOAD_PATH);
image.setImageBitmap(bm);

抱歉!评论已关闭.