、、、、、、、、、、、加权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
、、、、、、、、、、、、、代码
public class MainActivity extends AppCompatActivity {
private String url = "http://d.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fe871b36bad64034f79f019d4.jpg";
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
//实例化图片加载框架
BitmapUtils bitmapUtils=new BitmapUtils(this);
bitmapUtils.display(iv,url);
}
}
、、、、、、、、、、、、、、、、写两个utils类
public class BitmapUtils {
Context context;
//图片本地缓存路径
private static String SDCARD_CACHE= Environment.getExternalStorageDirectory()+"/imagecache";
//图片存放文件夹
File fileDir=new File(SDCARD_CACHE);
private Map<String, SoftReference<Bitmap>> map = new HashMap<String, SoftReference<Bitmap>>();
public BitmapUtils(Context context) {
this.context=context;
if (!fileDir.exists()){
fileDir.mkdir();
}
}
public void display(ImageView iv, String url) {
//内存缓存
Bitmap bitmap = loadMemory(url);
if (bitmap != null) {
iv.setImageBitmap(bitmap);
} else {
//sdcard缓存
bitmap = loadSD(url);
if (bitmap != null) {
iv.setImageBitmap(bitmap);
} else {
//网络缓存
LoadInterentImage(iv, url);
}
}
}
private Bitmap loadMemory(String url) {
SoftReference<Bitmap> value = map.get(url);
if (value != null) {
Bitmap bitmap = value.get();
return bitmap;
}
return null;
}
private Bitmap loadSD(String url) {
String name = getFileName(url);
File file = new File(fileDir, name);
if (file.exists()) {
//BitmapFactory选项
BitmapFactory.Options options = new BitmapFactory.Options();
//加载图片宽高
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(name, options);
//获取图片和手机屏幕宽高
int outWidth = options.outWidth;
int outHeight = options.outHeight;
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int widthPixels = displayMetrics.widthPixels;
int heightPixels = displayMetrics.heightPixels;
//图片跟手机屏幕进行比对
int scale = 0;
int scaleX = outWidth / widthPixels;
int scaleY = outHeight / heightPixels;
scale = scaleX > scaleY ? scaleX : scaleY;
if (scale == 0) {
scale = 1;
}
options.inJustDecodeBounds = false;
options.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
//内存缓存
SoftReference<Bitmap> value = new SoftReference<Bitmap>(bitmap);
map.put(name, value);
return bitmap;
}
return null;
}
//获取网络图片
private void LoadInterentImage(ImageView iv, String url) {
MyAsynTask task=new MyAsynTask(iv,url);
task.execute();
}
private class MyAsynTask extends AsyncTask<String,Integer,String>{
ImageView iv;
String url;
private InputStream inputStream;
private FileOutputStream fos;
public MyAsynTask(ImageView iv, String url) {
this.iv=iv;
this.url=url;
}
@Override
protected String doInBackground(String... params) {
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet(url);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode()==200){
inputStream = response.getEntity().getContent();
String name = getFileName(url);
File file=new File(fileDir,name);
fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
return fos.toString();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
fos.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//sdcard缓存
Bitmap bitmap = loadSD(s);
//ImageView转换成Bitmap转换成Bitmap
ImageViewToBitmap ivtb = new ImageViewToBitmap(iv, bitmap);
ivtb.iv.setImageBitmap(bitmap);
}
}
private String getFileName(String url) {
return Md5Utils.encode(url) + ".jpg";
}
//ImageView转换成Bitmap转换成Bitmap
private class ImageViewToBitmap {
ImageView iv;
Bitmap bitmap;
public ImageViewToBitmap(ImageView iv, Bitmap bitmap) {
this.iv = iv;
this.bitmap = bitmap;
}
}
}
、、、、、、、、
public class Md5Utils {
public static String encode(String password){
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] result = digest.digest(password.getBytes());
StringBuffer sb = new StringBuffer();
for(byte b : result){
int number = (int)(b & 0xff) ;
String str = Integer.toHexString(number);
if(str.length()==1){
sb.append("0");
}
sb.append(str);
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
//can't reach
return "";
}
}
}