/web-image-view/src/main/java/com/goal98/android/ImageCache.java

https://github.com/shangrz/FlipDroid · Java · 84 lines · 43 code · 15 blank · 26 comment · 3 complexity · 9b8c665c503309f0aa87dda438510df5 MD5 · raw file

  1. /* Copyright (c) 2009 Matthias Kaeppler
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package com.goal98.android;
  16. import android.graphics.Bitmap;
  17. import android.graphics.BitmapFactory;
  18. import com.goal98.android.CacheHelper;
  19. import java.io.*;
  20. /**
  21. * Implements a cache capable of caching it.tika.mongodb.image files. It exposes helper methods to immediately
  22. * access binary it.tika.mongodb.image data as {@link android.graphics.Bitmap} objects.
  23. *
  24. * @author Matthias Kaeppler
  25. */
  26. public class ImageCache extends com.goal98.android.AbstractCache<String, byte[]> {
  27. public ImageCache(int initialCapacity, long expirationInMinutes, int maxConcurrentThreads) {
  28. super("ImageCache", initialCapacity, expirationInMinutes, maxConcurrentThreads);
  29. }
  30. public synchronized void removeAllWithPrefix(String urlPrefix) {
  31. CacheHelper.removeAllWithStringPrefix(this, urlPrefix);
  32. }
  33. @Override
  34. public String getFileNameForKey(String imageUrl) {
  35. return CacheHelper.getFileNameFromUrl(imageUrl);
  36. }
  37. @Override
  38. protected byte[] readValueFromDisk(File file) throws IOException {
  39. BufferedInputStream istream = new BufferedInputStream(new FileInputStream(file));
  40. long fileSize = file.length();
  41. if (fileSize > Integer.MAX_VALUE) {
  42. throw new IOException("Cannot read files larger than " + Integer.MAX_VALUE + " bytes");
  43. }
  44. int imageDataLength = (int) fileSize;
  45. byte[] imageData = new byte[imageDataLength];
  46. istream.read(imageData, 0, imageDataLength);
  47. istream.close();
  48. return imageData;
  49. }
  50. public synchronized byte[] getBitmapBytes(Object elementKey) {
  51. byte[] imageData = super.get(elementKey);
  52. if (imageData == null) {
  53. return null;
  54. }
  55. // try {
  56. // final Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
  57. // return bitmap;
  58. // } catch (Throwable e) {
  59. // e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  60. // }
  61. return imageData;
  62. }
  63. @Override
  64. protected void writeValueToDisk(File file, byte[] imageData) throws IOException {
  65. BufferedOutputStream ostream = new BufferedOutputStream(new FileOutputStream(file));
  66. ostream.write(imageData);
  67. ostream.close();
  68. }
  69. }