Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. @Override
  2. public void onCreate() {
  3. super.onCreate();
  4. initUil();
  5. }
  6.  
  7. private void initUil() {
  8. DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
  9. .cacheInMemory(true)
  10. .cacheOnDisc(true)
  11. .build();
  12.  
  13. ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
  14. .taskExecutor(ThreadPool.getExecutorService())
  15. .defaultDisplayImageOptions(displayOptions)
  16. .build();
  17.  
  18. ImageLoader.getInstance().init(config);
  19. }
  20.  
  21. public class MediaCursorAdapter extends SimpleCursorAdapter implements Filterable {
  22. @Override
  23. public void bindView(View rowView, Context context, Cursor cursor) {
  24. String contentUri = getContentUri(cursor);
  25.  
  26. ImageView imgThumb = (ImageView) rowView.findViewById(R.id.imgThumb);
  27.  
  28. ImageLoader.getInstance().displayImage(contentUri, imgThumb);
  29. }
  30. }
  31.  
  32. ImageDecoder smartUriDecoder = new SmartUriDecoder(getContentResolver(), new BaseImageDecoder(false));
  33.  
  34. ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
  35. .taskExecutor(ThreadPool.getExecutorService())
  36. .defaultDisplayImageOptions(displayOptions)
  37. .imageDecoder(smartUriDecoder)
  38. .build();
  39.  
  40. public class SmartUriDecoder implements ImageDecoder {
  41. private final ContentResolver m_contentResolver;
  42. private final BaseImageDecoder m_imageUriDecoder;
  43.  
  44. public SmartUriDecoder(ContentResolver contentResolver, BaseImageDecoder imageUriDecoder) {
  45. if (imageUriDecoder == null) {
  46. throw new NullPointerException("Image decoder can't be null");
  47. }
  48.  
  49. m_contentResolver = contentResolver;
  50. m_imageUriDecoder = imageUriDecoder;
  51. }
  52.  
  53. @Override
  54. public Bitmap decode(ImageDecodingInfo info) throws IOException {
  55. if (TextUtils.isEmpty(info.getImageKey())) {
  56. return null;
  57. }
  58.  
  59. String cleanedUriString = cleanUriString(info.getImageKey());
  60. Uri uri = Uri.parse(cleanedUriString);
  61. if (isVideoUri(uri)) {
  62. return makeVideoThumbnail(info.getTargetSize().getWidth(), info.getTargetSize().getHeight(), getVideoFilePath(uri));
  63. }
  64. else {
  65. return m_imageUriDecoder.decode(info);
  66. }
  67. }
  68.  
  69. private Bitmap makeVideoThumbnail(int width, int height, String filePath) {
  70. if (filePath == null) {
  71. return null;
  72. }
  73. Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.MINI_KIND);
  74. Bitmap scaledThumb = scaleBitmap(thumbnail, width, height);
  75. thumbnail.recycle();
  76. return scaledThumb;
  77. }
  78.  
  79. private boolean isVideoUri(Uri uri) {
  80. String mimeType = m_contentResolver.getType(uri);
  81. return mimeType.startsWith("video/");
  82. }
  83.  
  84. private String getVideoFilePath(Uri uri) {
  85. String columnName = MediaStore.Video.VideoColumns.DATA;
  86. Cursor cursor = m_contentResolver.query(uri, new String[] { columnName }, null, null, null);
  87. try {
  88. int dataIndex = cursor.getColumnIndex(columnName);
  89. if (dataIndex != -1 && cursor.moveToFirst()) {
  90. return cursor.getString(dataIndex);
  91. }
  92. }
  93. finally {
  94. cursor.close();
  95. }
  96. return null;
  97. }
  98.  
  99. private Bitmap scaleBitmap(Bitmap origBitmap, int width, int height) {
  100. float scale = Math.min(
  101. ((float)width) / ((float)origBitmap.getWidth()),
  102. ((float)height) / ((float)origBitmap.getHeight())
  103. );
  104. return Bitmap.createScaledBitmap(origBitmap,
  105. (int)(((float)origBitmap.getWidth()) * scale),
  106. (int)(((float)origBitmap.getHeight()) * scale),
  107. false
  108. );
  109. }
  110.  
  111. private String cleanUriString(String contentUriWithAppendedSize) {
  112. // replace the size at the end of the URI with an empty string.
  113. // the URI will be in the form "content://....._256x256
  114. return contentUriWithAppendedSize.replaceFirst("_\d+x\d+$", "");
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement