MediaStoreImageFinder.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package epson.print.imgsel;
  2. import android.content.ContentResolver;
  3. import android.database.Cursor;
  4. import android.provider.MediaStore;
  5. import epson.print.Util.EPLog;
  6. import epson.print.imgsel.ImageFinder;
  7. import epson.print.imgsel.ImageFolderInfo;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11. import java.util.Collection;
  12. import java.util.Collections;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. public abstract class MediaStoreImageFinder implements ImageFinder {
  17. private static final String TAG = "MediaStoreImageFinder";
  18. protected abstract boolean myCheckFile(File file);
  19. public Collection<ImageFolderInfo> getFolderPhotoList(ImageFinder.Canceller canceller, ContentResolver contentResolver) {
  20. try {
  21. Cursor myQuery = myQuery(contentResolver);
  22. if (myQuery == null) {
  23. return null;
  24. }
  25. try {
  26. return getImageDirInfo(myQuery, canceller);
  27. } catch (Exception unused) {
  28. return null;
  29. } finally {
  30. myQuery.close();
  31. }
  32. } catch (Exception unused2) {
  33. return null;
  34. }
  35. }
  36. private List<ImageFolderInfo> getImageDirInfo(Cursor cursor, ImageFinder.Canceller canceller) {
  37. EPLog.d(TAG, "Enter getImageDirInfo");
  38. long currentTimeMillis = System.currentTimeMillis();
  39. int columnIndexOrThrow = cursor.getColumnIndexOrThrow("_data");
  40. int columnIndexOrThrow2 = cursor.getColumnIndexOrThrow("_id");
  41. if (!cursor.moveToFirst()) {
  42. return null;
  43. }
  44. HashMap hashMap = new HashMap();
  45. int i = 0;
  46. while (!canceller.checkCanceled()) {
  47. String string = cursor.getString(columnIndexOrThrow);
  48. if (string != null) {
  49. long j = cursor.getLong(columnIndexOrThrow2);
  50. try {
  51. File absoluteFile = new File(string).getAbsoluteFile();
  52. String parent = absoluteFile.getParent();
  53. if (parent != null) {
  54. if (myCheckFile(absoluteFile)) {
  55. ImageFolderInfo imageFolderInfo = (ImageFolderInfo) hashMap.get(parent);
  56. if (imageFolderInfo == null) {
  57. imageFolderInfo = new ImageFolderInfo(parent);
  58. hashMap.put(parent, imageFolderInfo);
  59. }
  60. imageFolderInfo.addImageLastIfPossible(j, absoluteFile.toString(), i);
  61. }
  62. i++;
  63. } else {
  64. throw new IOException();
  65. }
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. if (!cursor.moveToNext()) {
  71. long currentTimeMillis2 = System.currentTimeMillis();
  72. EPLog.d(TAG, "Leave getImageDirInfo time = " + (currentTimeMillis2 - currentTimeMillis) + " msec");
  73. return convertTempMapToSortedList(hashMap);
  74. }
  75. }
  76. return null;
  77. }
  78. private List<ImageFolderInfo> convertTempMapToSortedList(Map<String, ImageFolderInfo> map) {
  79. if (map == null) {
  80. return null;
  81. }
  82. ArrayList arrayList = new ArrayList(map.values());
  83. Collections.sort(arrayList, new ImageFolderInfo.DatabaseOrderComparator());
  84. return arrayList;
  85. }
  86. public List<ImageFileInfo> findImageInDirectory(String str, ContentResolver contentResolver, ImageFinder.Canceller canceller) {
  87. try {
  88. Cursor myQuery = myQuery(contentResolver);
  89. if (myQuery == null) {
  90. return null;
  91. }
  92. try {
  93. return getImageFileInfoList(str, myQuery, canceller);
  94. } catch (Exception unused) {
  95. return null;
  96. } finally {
  97. myQuery.close();
  98. }
  99. } catch (Exception unused2) {
  100. return null;
  101. }
  102. }
  103. private List<ImageFileInfo> getImageFileInfoList(String str, Cursor cursor, ImageFinder.Canceller canceller) {
  104. if (str == null || cursor == null || canceller == null) {
  105. return null;
  106. }
  107. ArrayList arrayList = new ArrayList();
  108. int columnIndexOrThrow = cursor.getColumnIndexOrThrow("_data");
  109. int columnIndexOrThrow2 = cursor.getColumnIndexOrThrow("_id");
  110. if (!cursor.moveToFirst()) {
  111. return null;
  112. }
  113. while (!canceller.checkCanceled()) {
  114. String string = cursor.getString(columnIndexOrThrow);
  115. if (string != null) {
  116. File file = new File(string);
  117. if (str.equals(file.getParent()) && myCheckFile(file)) {
  118. arrayList.add(new ImageFileInfo(file.toString(), cursor.getLong(columnIndexOrThrow2)));
  119. }
  120. }
  121. if (!cursor.moveToNext()) {
  122. return arrayList;
  123. }
  124. }
  125. return null;
  126. }
  127. private Cursor myQuery(ContentResolver contentResolver) {
  128. if (contentResolver == null) {
  129. return null;
  130. }
  131. return contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "_data"}, (String) null, (String[]) null, "date_modified DESC");
  132. }
  133. }