AvatarHelper.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. package com.xzjmyk.pm.activity.helper;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.os.Handler;
  5. import android.os.Looper;
  6. import android.text.TextUtils;
  7. import android.util.Base64;
  8. import android.util.Log;
  9. import android.widget.ImageView;
  10. import com.nostra13.universalimageloader.core.ImageLoader;
  11. import com.nostra13.universalimageloader.core.assist.MemoryCacheUtil;
  12. import com.nostra13.universalimageloader.core.imageaware.ImageAware;
  13. import com.xzjmyk.pm.activity.MyApplication;
  14. import com.xzjmyk.pm.activity.ui.erp.util.LogUtil;
  15. import java.io.ByteArrayOutputStream;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.net.HttpURLConnection;
  20. import java.net.MalformedURLException;
  21. import java.net.URL;
  22. import java.net.URLConnection;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. /**
  27. * 用户头像的上传和获取
  28. */
  29. public class AvatarHelper {
  30. private Map<String, Long> mCheckTimeMaps;
  31. private Handler mHandler;
  32. private AvatarHelper() {
  33. mCheckTimeMaps = new HashMap<String, Long>();
  34. mHandler = new Handler(Looper.getMainLooper());
  35. }
  36. public static AvatarHelper INSTANCE;
  37. public static AvatarHelper getInstance() {
  38. if (INSTANCE == null) {
  39. synchronized (AvatarHelper.class) {
  40. if (INSTANCE == null) {
  41. INSTANCE = new AvatarHelper();
  42. }
  43. }
  44. }
  45. return INSTANCE;
  46. }
  47. /**
  48. * 当自己上传了新的头像了,立即删除缓存
  49. *
  50. * @param userId
  51. */
  52. public void deleteAvatar(String userId) {
  53. final String url1 = getAvatarUrl(userId, true);
  54. final String url2 = getAvatarUrl(userId, false);
  55. if (!TextUtils.isEmpty(url1)) {
  56. deleteCache(url1);
  57. }
  58. if (!TextUtils.isEmpty(url2)) {
  59. deleteCache(url2);
  60. }
  61. }
  62. /**
  63. * 删除缓存在本地和内存中的图片
  64. *
  65. * @param url
  66. */
  67. private void deleteCache(String url) {
  68. try {
  69. final File localFile = ImageLoader.getInstance().getDiscCache().get(url);
  70. if (localFile != null && localFile.exists()) {
  71. localFile.delete();
  72. }
  73. List<String> keys = MemoryCacheUtil.findCacheKeysForImageUri(url, ImageLoader.getInstance().getMemoryCache());
  74. if (keys != null && keys.size() > 0) {
  75. for (String key : keys) {
  76. ImageLoader.getInstance().getMemoryCache().remove(key);
  77. }
  78. }
  79. } catch (NullPointerException e) {
  80. } finally {
  81. }
  82. // if (ImageLoader.getInstance().getDiscCache() == null)
  83. // return;
  84. }
  85. public void displayRoomAvatar(String userId, final ImageView imageView, final boolean isThumb) {
  86. final String url = getAvatarUrl(userId, isThumb);
  87. if (TextUtils.isEmpty(url)) {
  88. return;
  89. }
  90. Long lastCheckTime = mCheckTimeMaps.get(url);
  91. if (lastCheckTime == null || System.currentTimeMillis() - lastCheckTime > 0.1 * 60 * 1000) {// 至少间隔5分钟检测一下
  92. new Thread(new Runnable() {
  93. @Override
  94. public void run() {
  95. long lastModifyTime = getLastModify(url);
  96. long localLastModified = 0;
  97. final File localFile = ImageLoader.getInstance().getDiscCache().get(url);
  98. if (localFile != null && localFile.exists()) {
  99. localLastModified = localFile.lastModified();
  100. }
  101. final boolean delete = localLastModified < lastModifyTime;
  102. mHandler.post(new Runnable() {
  103. @Override
  104. public void run() {
  105. mCheckTimeMaps.put(url, System.currentTimeMillis());
  106. if (delete) {
  107. if (localFile != null) {
  108. localFile.delete();
  109. }
  110. List<String> keys = MemoryCacheUtil.findCacheKeysForImageUri(url, ImageLoader.getInstance().getMemoryCache());
  111. if (keys != null && keys.size() > 0) {
  112. for (String key : keys) {
  113. ImageLoader.getInstance().getMemoryCache().remove(key);
  114. }
  115. }
  116. }
  117. display(url, imageView, isThumb);
  118. }
  119. });
  120. }
  121. }).start();
  122. } else {
  123. display(url, imageView, isThumb);
  124. }
  125. }
  126. public void displayAvatar(String userId, final ImageView imageView, final boolean isThumb) {
  127. final String url = getAvatarUrl(userId, isThumb);
  128. if (TextUtils.isEmpty(url)) {
  129. return;
  130. }
  131. Long lastCheckTime = mCheckTimeMaps.get(url);
  132. if (lastCheckTime == null || System.currentTimeMillis() - lastCheckTime > 5 * 60 * 1000) {// 至少间隔5分钟检测一下
  133. new Thread(new Runnable() {
  134. @Override
  135. public void run() {
  136. long lastModifyTime = getLastModify(url);
  137. long localLastModified = 0;
  138. final File localFile = ImageLoader.getInstance().getDiscCache().get(url);
  139. if (localFile != null && localFile.exists()) {
  140. localLastModified = localFile.lastModified();
  141. }
  142. final boolean delete = localLastModified < lastModifyTime;
  143. mHandler.post(new Runnable() {
  144. @Override
  145. public void run() {
  146. mCheckTimeMaps.put(url, System.currentTimeMillis());
  147. if (delete) {
  148. if (localFile != null) {
  149. localFile.delete();
  150. }
  151. List<String> keys = MemoryCacheUtil.findCacheKeysForImageUri(url, ImageLoader.getInstance().getMemoryCache());
  152. if (keys != null && keys.size() > 0) {
  153. for (String key : keys) {
  154. ImageLoader.getInstance().getMemoryCache().remove(key);
  155. }
  156. }
  157. }
  158. display(url, imageView, isThumb);
  159. }
  160. });
  161. }
  162. }).start();
  163. } else {
  164. display(url, imageView, isThumb);
  165. }
  166. }
  167. public void displayAvatarPng(String userId, final ImageView imageView, final boolean isThumb) {
  168. final String url = getAvatarUrlPng(userId, isThumb);
  169. Log.i("Arison", "AvatarHelper:displayAvatar:140:" + url);
  170. if (TextUtils.isEmpty(url)) {
  171. return;
  172. }
  173. // imageView.setTag(url);
  174. Long lastCheckTime = mCheckTimeMaps.get(url);
  175. if (lastCheckTime == null || System.currentTimeMillis() - lastCheckTime > 5 * 60 * 1000) {// 至少间隔5分钟检测一下
  176. new Thread(new Runnable() {
  177. @Override
  178. public void run() {
  179. long lastModifyTime = getLastModify(url);
  180. long localLastModified = 0;
  181. final File localFile = ImageLoader.getInstance().getDiscCache().get(url);
  182. if (localFile != null && localFile.exists()) {
  183. localLastModified = localFile.lastModified();
  184. }
  185. final boolean delete = localLastModified < lastModifyTime;
  186. mHandler.post(new Runnable() {
  187. @Override
  188. public void run() {
  189. mCheckTimeMaps.put(url, System.currentTimeMillis());
  190. if (delete) {
  191. if (localFile != null) {
  192. localFile.delete();
  193. }
  194. List<String> keys = MemoryCacheUtil.findCacheKeysForImageUri(url, ImageLoader.getInstance().getMemoryCache());
  195. if (keys != null && keys.size() > 0) {
  196. for (String key : keys) {
  197. ImageLoader.getInstance().getMemoryCache().remove(key);
  198. }
  199. }
  200. }
  201. display(url, imageView, isThumb);
  202. }
  203. });
  204. }
  205. }).start();
  206. } else {
  207. display(url, imageView, isThumb);
  208. }
  209. }
  210. private long getLastModify(String url) {
  211. HttpURLConnection connection = null;
  212. try {
  213. connection = (HttpURLConnection) new URL(url).openConnection();
  214. } catch (MalformedURLException e) {
  215. e.printStackTrace();
  216. } catch (IOException e) {
  217. e.printStackTrace();
  218. }
  219. if (connection == null) {
  220. return 0;
  221. } else {
  222. connection.setDoOutput(false);
  223. connection.setDoInput(true);
  224. try {
  225. connection.connect();
  226. return connection.getLastModified();
  227. } catch (IOException e) {
  228. e.printStackTrace();
  229. } finally {
  230. connection.disconnect();
  231. }
  232. }
  233. return 0;
  234. }
  235. public void display(final String url, ImageView imageView, boolean isThumb) {
  236. if (isThumb) {
  237. ImageLoader.getInstance().displayImage(url, imageView, MyApplication.mAvatarRoundImageOptions);
  238. } else {
  239. ImageLoader.getInstance().displayImage(url, imageView, MyApplication.mAvatarNormalImageOptions);
  240. }
  241. }
  242. public void display(String userId, final ImageView imageView, final boolean isThumb, boolean isDeleteCache) {
  243. final String url = getAvatarUrl(userId, isThumb);
  244. LogUtil.d(url);
  245. if (isDeleteCache) {
  246. deleteCache(url);
  247. }
  248. if (isThumb) {
  249. ImageLoader.getInstance().displayImage(url, imageView, MyApplication.mAvatarRoundImageOptions);
  250. } else {
  251. ImageLoader.getInstance().displayImage(url, imageView, MyApplication.mAvatarNormalImageOptions);
  252. }
  253. }
  254. //根据loginUserId,获取该用户头像的Bitmap对象
  255. //Caused by: Android.os.NetworkOnMainThreadException,查了下原因上在4.0之后在主线程里面执行Http请求都会报这个错,大概是怕Http请求时间太长造成程序假死的情况吧。
  256. /**
  257. * 图片的url 转 Bitmap对象
  258. *
  259. * @param userId
  260. * @param isThumb
  261. * @return
  262. */
  263. public static Bitmap returnBitmap(String userId, boolean isThumb) {
  264. final String urlpath = getAvatarUrl(userId, isThumb);
  265. Bitmap mBitmap = null;
  266. try {
  267. URL url = new URL(urlpath);
  268. URLConnection conn = url.openConnection();
  269. conn.connect();
  270. InputStream in;
  271. in = conn.getInputStream();
  272. mBitmap = BitmapFactory.decodeStream(in);
  273. } catch (IOException e) {
  274. e.printStackTrace();
  275. }
  276. return mBitmap;
  277. }
  278. /**
  279. * 图片的 Bitmap对象转 url
  280. *
  281. * @param bitmap
  282. * @return
  283. */
  284. public static String doBitmapTurnToStringurl(Bitmap bitmap) {
  285. ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream
  286. bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
  287. byte[] appicon = baos.toByteArray();// 转为byte数组
  288. return Base64.encodeToString(appicon, Base64.DEFAULT);
  289. }
  290. private void display(String url, ImageAware imageAware, boolean isThumb) {
  291. if (isThumb) {
  292. ImageLoader.getInstance().displayImage(url, imageAware, MyApplication.mAvatarRoundImageOptions);
  293. } else {
  294. ImageLoader.getInstance().displayImage(url, imageAware, MyApplication.mAvatarNormalImageOptions);
  295. }
  296. }
  297. public void displayAvatar(String userId, final ImageAware imageAware, final boolean isThumb) {
  298. final String url = getAvatarUrl(userId, isThumb);
  299. if (TextUtils.isEmpty(url)) {
  300. return;
  301. }
  302. Long lastCheckTime = mCheckTimeMaps.get(url);
  303. if (lastCheckTime == null || System.currentTimeMillis() - lastCheckTime > 1 * 60 * 1000) {
  304. new Thread(new Runnable() {
  305. @Override
  306. public void run() {
  307. Log.d("wang", "lastChecttime<<<<");
  308. long lastModifyTime = getLastModify(url);
  309. long localLastModified = 0;
  310. final File localFile = ImageLoader.getInstance().getDiscCache().get(url);
  311. if (localFile != null && localFile.exists()) {
  312. localLastModified = localFile.lastModified();
  313. }
  314. final boolean delete = localLastModified < lastModifyTime;
  315. mHandler.post(new Runnable() {
  316. @Override
  317. public void run() {
  318. mCheckTimeMaps.put(url, System.currentTimeMillis());
  319. if (delete) {
  320. if (localFile != null) {
  321. localFile.delete();
  322. }
  323. List<String> keys = MemoryCacheUtil.findCacheKeysForImageUri(url, ImageLoader.getInstance().getMemoryCache());
  324. if (keys != null && keys.size() > 0) {
  325. for (String key : keys) {
  326. ImageLoader.getInstance().getMemoryCache().remove(key);
  327. }
  328. }
  329. }
  330. display(url, imageAware, isThumb);
  331. }
  332. });
  333. }
  334. }).start();
  335. } else {
  336. display(url, imageAware, isThumb);
  337. }
  338. }
  339. public static String getRoomAvatarUrl(String userId, boolean isThumb) {
  340. String url = null;
  341. if (isThumb) {
  342. url = MyApplication.getInstance().getConfig().AVATAR_THUMB_PREFIX + "/" + "/" + userId + ".jpg";
  343. } else {
  344. url = MyApplication.getInstance().getConfig().AVATAR_ORIGINAL_PREFIX + "/" + "/" + userId + ".jpg";
  345. }
  346. return url;
  347. }
  348. public static String getAvatarUrl(String userId, boolean isThumb) {
  349. if (TextUtils.isEmpty(userId)) {
  350. return null;
  351. }
  352. int userIdInt = -1;
  353. try {
  354. userIdInt = Integer.parseInt(userId);
  355. } catch (NumberFormatException e) {
  356. e.printStackTrace();
  357. }
  358. if (userIdInt == -1 || userIdInt == 0) {
  359. return null;
  360. }
  361. int dirName = userIdInt % 10000;
  362. String url = null;
  363. if (isThumb) {
  364. url = MyApplication.getInstance().getConfig().AVATAR_THUMB_PREFIX + "/" + dirName + "/" + userId + ".jpg";
  365. } else {
  366. url = MyApplication.getInstance().getConfig().AVATAR_ORIGINAL_PREFIX + "/" + dirName + "/" + userId + ".jpg";
  367. }
  368. return url;
  369. }
  370. public static String getAvatarUrlPng(String userId, boolean isThumb) {
  371. if (TextUtils.isEmpty(userId)) {
  372. return null;
  373. }
  374. int userIdInt = -1;
  375. try {
  376. userIdInt = Integer.parseInt(userId);
  377. } catch (NumberFormatException e) {
  378. e.printStackTrace();
  379. }
  380. if (userIdInt == -1 || userIdInt == 0) {
  381. return null;
  382. }
  383. int dirName = userIdInt % 10000;
  384. String url = null;
  385. if (isThumb) {
  386. url = MyApplication.getInstance().getConfig().AVATAR_THUMB_PREFIX + "/" + dirName + "/" + userId + ".png";
  387. } else {
  388. url = MyApplication.getInstance().getConfig().AVATAR_ORIGINAL_PREFIX + "/" + dirName + "/" + userId + ".png";
  389. }
  390. return url;
  391. }
  392. // // 无用
  393. // public void displayResumeAvatar(String userId, ImageView imageView, boolean isThumb) {
  394. // String url = getResumeAvatar(userId, isThumb);
  395. // if (TextUtils.isEmpty(url)) {
  396. // return;
  397. // }
  398. // // Bitmap bitmap = ImageLoader.getInstance().getMemoryCache().get(url);
  399. // // if (bitmap != null && !bitmap.isRecycled()) {
  400. // // ImageLoader.getInstance().displayImage(url, imageView,
  401. // // MyApplication.mRoundImageOptions);
  402. // // return;
  403. // // }
  404. // //
  405. // // File file = ImageLoader.getInstance().getDiscCache().get(url);
  406. // // if (file != null && file.exists()) {
  407. // // long localLastModified=file.lastModified();
  408. // // if(System.currentTimeMillis()-localLastModified>24L*60*60*1000){//图片过期
  409. // //
  410. // // }
  411. // // }
  412. //
  413. // if (isThumb) {
  414. // ImageLoader.getInstance().displayImage(url, imageView, MyApplication.mAvatarRoundImageOptions);
  415. // } else {
  416. // ImageLoader.getInstance().displayImage(url, imageView, MyApplication.mAvatarNormalImageOptions);
  417. // }
  418. // }
  419. //
  420. // // 无用
  421. // public void displayResumeAvatar(String userId, ImageAware imageAware, boolean isThumb) {
  422. // String url = getResumeAvatar(userId, isThumb);
  423. // if (TextUtils.isEmpty(url)) {
  424. // return;
  425. // }
  426. // // Bitmap bitmap = ImageLoader.getInstance().getMemoryCache().get(url);
  427. // // if (bitmap != null && !bitmap.isRecycled()) {
  428. // // ImageLoader.getInstance().displayImage(url, imageView,
  429. // // MyApplication.mRoundImageOptions);
  430. // // return;
  431. // // }
  432. // //
  433. // // File file = ImageLoader.getInstance().getDiscCache().get(url);
  434. // // if (file != null && file.exists()) {
  435. // // long localLastModified=file.lastModified();
  436. // // if(System.currentTimeMillis()-localLastModified>24L*60*60*1000){//图片过期
  437. // //
  438. // // }
  439. // // }
  440. //
  441. // if (isThumb) {
  442. // ImageLoader.getInstance().displayImage(url, imageAware, MyApplication.mAvatarRoundImageOptions);
  443. // } else {
  444. // ImageLoader.getInstance().displayImage(url, imageAware, MyApplication.mAvatarNormalImageOptions);
  445. // }
  446. // }
  447. //
  448. // // 无用
  449. // public static String getResumeAvatar(String userId, boolean isThumb) {
  450. // if (TextUtils.isEmpty(userId)) {
  451. // return null;
  452. // }
  453. // int userIdInt = -1;
  454. // try {
  455. // userIdInt = Integer.parseInt(userId);
  456. // } catch (NumberFormatException e) {
  457. // e.printStackTrace();
  458. // }
  459. // if (userIdInt == -1 || userIdInt == 0) {
  460. // return null;
  461. // }
  462. //
  463. // int dirName = userIdInt % 10000;
  464. // String url = null;
  465. // if (isThumb) {
  466. // url = MyApplication.getInstance().getConfig().RESUME_AVATAR_THUMB_PREFIX + "/" + dirName + "/" + userId + ".jpg";
  467. // } else {
  468. // url = MyApplication.getInstance().getConfig().RESUME_AVATAR_ORIGINAL_PREFIX + "/" + dirName + "/" + userId + ".jpg";
  469. // }
  470. // return url;
  471. // }
  472. }