Commit 3d5fafab29caed7fa8f3e036c681aca186947fa4
1 parent
74a9341c
图片视频选取功能优化,解决读取媒体列表ANR问题,解决读取过程中退出页面闪退,解决权限判定问题
Showing
9 changed files
with
179 additions
and
224 deletions
config.gradle
lib_media/src/main/java/com/cnlive/media/media/DataCallback.java deleted
100644 → 0
lib_media/src/main/java/com/cnlive/media/media/entity/Media.java
| ... | ... | @@ -14,7 +14,7 @@ import java.util.Objects; |
| 14 | 14 | * <p> |
| 15 | 15 | * 简 介 : 媒体数据的数据模型 |
| 16 | 16 | */ |
| 17 | -public class Media implements Parcelable { | |
| 17 | +public class Media implements Parcelable { | |
| 18 | 18 | public String path; |
| 19 | 19 | public String name; |
| 20 | 20 | public long time; |
| ... | ... | @@ -74,7 +74,6 @@ public class Media implements Parcelable { |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | |
| 77 | - | |
| 78 | 77 | public Media(int mediaType) { |
| 79 | 78 | this.mediaType = mediaType; |
| 80 | 79 | } |
| ... | ... | @@ -174,6 +173,7 @@ public class Media implements Parcelable { |
| 174 | 173 | public void setReturnUri(boolean isReturnUri) { |
| 175 | 174 | this.isReturnUri = isReturnUri; |
| 176 | 175 | } |
| 176 | + | |
| 177 | 177 | public boolean isSelect() { |
| 178 | 178 | return isSelect; |
| 179 | 179 | } |
| ... | ... | @@ -189,17 +189,40 @@ public class Media implements Parcelable { |
| 189 | 189 | Media media = (Media) o; |
| 190 | 190 | return |
| 191 | 191 | mediaType == media.mediaType && |
| 192 | - size == media.size && | |
| 193 | - id == media.id && | |
| 194 | - duration == media.duration && | |
| 192 | + size == media.size && | |
| 193 | + id == media.id && | |
| 194 | + duration == media.duration && | |
| 195 | 195 | // Objects.equals(path, media.path) && |
| 196 | - Objects.equals(name, media.name) && | |
| 197 | - Objects.equals(parentDir, media.parentDir) && | |
| 198 | - Objects.equals(fileUri, media.fileUri); | |
| 196 | + Objects.equals(name, media.name) && | |
| 197 | + Objects.equals(parentDir, media.parentDir) && | |
| 198 | + Objects.equals(fileUri, media.fileUri); | |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | @Override |
| 202 | 202 | public int hashCode() { |
| 203 | 203 | return Objects.hash(path, name, mediaType, size, id, parentDir, duration, fileUri); |
| 204 | 204 | } |
| 205 | + | |
| 206 | + public Media copy() { | |
| 207 | + Media newData = new Media(mediaType); | |
| 208 | + newData.path = path; | |
| 209 | + newData.name = name; | |
| 210 | + newData.time = time; | |
| 211 | + newData.mediaType = mediaType; | |
| 212 | + newData.size = size; | |
| 213 | + newData.id = id; | |
| 214 | + newData.parentDir = parentDir; | |
| 215 | + newData.isDeleted = isDeleted; | |
| 216 | + newData.duration = duration; | |
| 217 | + newData.compressed = compressed; | |
| 218 | + newData.isReturnUri = isReturnUri; | |
| 219 | + newData.fileUri = fileUri; | |
| 220 | + newData.thumbnails = thumbnails; | |
| 221 | + newData.isSelect = isSelect; | |
| 222 | + newData.imgWidth = imgWidth; | |
| 223 | + newData.imgHeight = imgHeight; | |
| 224 | + newData.isLongImg = isLongImg; | |
| 225 | + newData.isFail = isFail; | |
| 226 | + return newData; | |
| 227 | + } | |
| 205 | 228 | } | ... | ... |
lib_media/src/main/java/com/cnlive/media/media/loader/ImageLoader.java
| 1 | 1 | package com.cnlive.media.media.loader; |
| 2 | 2 | |
| 3 | -import android.app.LoaderManager; | |
| 4 | 3 | import android.content.Context; |
| 5 | -import android.content.CursorLoader; | |
| 6 | -import android.content.Loader; | |
| 7 | 4 | import android.database.Cursor; |
| 8 | 5 | import android.net.Uri; |
| 9 | -import android.os.Bundle; | |
| 10 | 6 | import android.provider.MediaStore; |
| 11 | 7 | import android.text.TextUtils; |
| 12 | 8 | |
| 13 | -import com.cnlive.media.media.DataCallback; | |
| 9 | +import com.cnlive.media.R; | |
| 14 | 10 | import com.cnlive.media.media.entity.Folder; |
| 15 | 11 | import com.cnlive.media.media.entity.Media; |
| 16 | 12 | import com.cnlive.media.utils.MediaUtils; |
| 17 | -import com.cnlive.media.R; | |
| 18 | 13 | |
| 19 | 14 | import java.io.File; |
| 20 | 15 | import java.util.ArrayList; |
| ... | ... | @@ -23,8 +18,8 @@ import java.util.ArrayList; |
| 23 | 18 | * Created by dmcBig on 2017/7/3. |
| 24 | 19 | */ |
| 25 | 20 | |
| 26 | -public class ImageLoader extends LoaderM implements LoaderManager.LoaderCallbacks { | |
| 27 | - private static ArrayList<Folder> folders=new ArrayList<>(); | |
| 21 | +public class ImageLoader extends LoaderM { | |
| 22 | + private static ArrayList<Folder> folders = new ArrayList<>(); | |
| 28 | 23 | private final String TAG = this.getClass().getName(); |
| 29 | 24 | String[] IMAGE_PROJECTION = { |
| 30 | 25 | MediaStore.Images.Media.DATA, |
| ... | ... | @@ -37,60 +32,39 @@ public class ImageLoader extends LoaderM implements LoaderManager.LoaderCallback |
| 37 | 32 | MediaStore.Images.Media._ID}; |
| 38 | 33 | |
| 39 | 34 | Context mContext; |
| 40 | - DataCallback mLoader; | |
| 41 | 35 | |
| 42 | 36 | private static ImageLoader instance = null; |
| 43 | 37 | |
| 44 | - public synchronized static ImageLoader getInstance(Context context, DataCallback loader) { | |
| 38 | + public synchronized static ImageLoader getInstance(Context context) { | |
| 45 | 39 | if (instance == null || folders == null) { |
| 46 | 40 | instance = new ImageLoader(context); |
| 47 | 41 | folders = new ArrayList<>(); |
| 48 | 42 | } |
| 49 | - instance.setmLoader(loader); | |
| 43 | + | |
| 50 | 44 | return instance; |
| 51 | 45 | } |
| 52 | 46 | |
| 47 | + String selection; | |
| 48 | + Uri queryUri; | |
| 49 | + | |
| 53 | 50 | public ImageLoader(Context context) { |
| 54 | 51 | this.mContext = context; |
| 55 | - } | |
| 56 | - | |
| 57 | - public void setmLoader(DataCallback mLoader) { | |
| 58 | - this.mLoader = mLoader; | |
| 59 | - } | |
| 60 | - | |
| 61 | -// public ImageLoader(Context context, DataCallback loader) { | |
| 62 | -// this.mContext = context; | |
| 63 | -// this.mLoader = loader; | |
| 64 | -// } | |
| 65 | - | |
| 66 | - @Override | |
| 67 | - public Loader onCreateLoader(int picker_type, Bundle bundle) { | |
| 68 | - String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "=" | |
| 52 | + selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "=" | |
| 69 | 53 | + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE; |
| 70 | - Uri queryUri = MediaStore.Files.getContentUri("external"); | |
| 71 | - CursorLoader cursorLoader = new CursorLoader( | |
| 72 | - mContext, | |
| 73 | - queryUri, | |
| 74 | - IMAGE_PROJECTION, | |
| 75 | - selection, | |
| 76 | - null, // Selection args (none). | |
| 77 | - MediaStore.Images.Media.DATE_ADDED + " DESC" // Sort order. | |
| 78 | - ); | |
| 79 | - return cursorLoader; | |
| 54 | + queryUri = MediaStore.Files.getContentUri("external"); | |
| 80 | 55 | } |
| 81 | 56 | |
| 82 | - @Override | |
| 83 | - public void onLoadFinished(Loader loader, Object o) { | |
| 84 | - if (folders == null || mLoader == null || instance == null) return; | |
| 85 | - if (folders.size() > 0) { | |
| 86 | - mLoader.onData(folders); | |
| 87 | - return; | |
| 57 | + | |
| 58 | + public ArrayList<Folder> load() { | |
| 59 | + if (folders == null || instance == null) return null; | |
| 60 | + if (!folders.isEmpty()) { | |
| 61 | + return folders; | |
| 88 | 62 | } |
| 89 | 63 | |
| 90 | 64 | Folder allFolder = new Folder(mContext.getResources().getString(R.string.all_image)); |
| 91 | 65 | folders.add(allFolder); |
| 92 | - Cursor cursor = null; | |
| 93 | - if (o != null) cursor = (Cursor) o; | |
| 66 | + Cursor cursor = mContext.getContentResolver().query(queryUri, IMAGE_PROJECTION, selection, null, MediaStore.Images.Media.DATE_ADDED + " DESC"); | |
| 67 | + | |
| 94 | 68 | while (cursor != null && cursor.moveToNext()) { |
| 95 | 69 | String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)); |
| 96 | 70 | File file = new File(path); |
| ... | ... | @@ -139,19 +113,14 @@ public class ImageLoader extends LoaderM implements LoaderManager.LoaderCallback |
| 139 | 113 | folders.add(folder); |
| 140 | 114 | } |
| 141 | 115 | } |
| 142 | - if (mLoader != null) mLoader.onData(folders); | |
| 143 | -// Log.e(TAG, "数据为空: " + folders.size()); | |
| 144 | - if (cursor != null) cursor.close(); | |
| 145 | - } | |
| 146 | 116 | |
| 147 | - @Override | |
| 148 | - public void onLoaderReset(Loader loader) { | |
| 117 | + if (cursor != null) cursor.close(); | |
| 118 | + return folders; | |
| 149 | 119 | |
| 150 | 120 | } |
| 151 | 121 | |
| 152 | 122 | |
| 153 | - @Override | |
| 154 | - public void onDestory() { | |
| 123 | + public void destroy() { | |
| 155 | 124 | if (null != instance && null != folders) { |
| 156 | 125 | folders.clear(); |
| 157 | 126 | folders = null; | ... | ... |
lib_media/src/main/java/com/cnlive/media/media/loader/LoaderM.java
lib_media/src/main/java/com/cnlive/media/media/loader/MediaLoader.java
| 1 | 1 | package com.cnlive.media.media.loader; |
| 2 | 2 | |
| 3 | -import android.app.LoaderManager; | |
| 4 | -import android.content.ContentResolver; | |
| 5 | 3 | import android.content.Context; |
| 6 | -import android.content.CursorLoader; | |
| 7 | -import android.content.Loader; | |
| 8 | 4 | import android.database.Cursor; |
| 9 | -import android.graphics.Bitmap; | |
| 10 | 5 | import android.net.Uri; |
| 11 | -import android.os.Bundle; | |
| 12 | 6 | import android.provider.MediaStore; |
| 13 | 7 | import android.text.TextUtils; |
| 14 | -import android.util.Log; | |
| 15 | -import android.util.Size; | |
| 16 | 8 | |
| 17 | -import com.cnlive.media.media.DataCallback; | |
| 9 | +import com.cnlive.media.R; | |
| 18 | 10 | import com.cnlive.media.media.entity.Folder; |
| 19 | 11 | import com.cnlive.media.media.entity.Media; |
| 20 | -import com.cnlive.media.utils.AndroidQUtil; | |
| 21 | 12 | import com.cnlive.media.utils.MediaUtils; |
| 22 | -import com.cnlive.media.R; | |
| 23 | 13 | |
| 24 | 14 | import java.io.File; |
| 25 | 15 | import java.util.ArrayList; |
| ... | ... | @@ -29,7 +19,7 @@ import java.util.ArrayList; |
| 29 | 19 | * Created by dmcBig on 2017/6/9. |
| 30 | 20 | */ |
| 31 | 21 | |
| 32 | -public class MediaLoader extends LoaderM implements LoaderManager.LoaderCallbacks { | |
| 22 | +public class MediaLoader extends LoaderM { | |
| 33 | 23 | private final String TAG = this.getClass().getName(); |
| 34 | 24 | |
| 35 | 25 | String[] MEDIA_PROJECTION = { |
| ... | ... | @@ -45,64 +35,44 @@ public class MediaLoader extends LoaderM implements LoaderManager.LoaderCallback |
| 45 | 35 | MediaStore.Video.VideoColumns.DURATION}; |
| 46 | 36 | |
| 47 | 37 | Context mContext; |
| 48 | - DataCallback mLoader; | |
| 38 | + | |
| 49 | 39 | private Uri photoUri; |
| 50 | 40 | private static MediaLoader instance; |
| 51 | 41 | private static ArrayList<Folder> folders = new ArrayList<>(); |
| 52 | 42 | |
| 53 | - public synchronized static MediaLoader getInstance(Context context, DataCallback loader) { | |
| 43 | + public synchronized static MediaLoader getInstance(Context context) { | |
| 54 | 44 | if (instance == null) { |
| 55 | 45 | instance = new MediaLoader(context); |
| 56 | 46 | folders = new ArrayList<>(); |
| 57 | 47 | } |
| 58 | - instance.setmLoader(loader); | |
| 59 | 48 | return instance; |
| 60 | 49 | } |
| 61 | 50 | |
| 51 | + String selection; | |
| 52 | + Uri queryUri; | |
| 53 | + | |
| 62 | 54 | public MediaLoader(Context context) { |
| 63 | 55 | if (context == null) return; |
| 64 | - this.mContext = context; | |
| 65 | - } | |
| 66 | - | |
| 67 | - public void setmLoader(DataCallback mLoader) { | |
| 68 | - this.mLoader = mLoader; | |
| 69 | - } | |
| 70 | 56 | |
| 71 | - @Override | |
| 72 | - public Loader onCreateLoader(int picker_type, Bundle bundle) { | |
| 73 | - String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "=" | |
| 57 | + selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "=" | |
| 74 | 58 | + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE |
| 75 | 59 | + " OR " |
| 76 | 60 | + MediaStore.Files.FileColumns.MEDIA_TYPE + "=" |
| 77 | 61 | + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO; |
| 78 | - | |
| 79 | - | |
| 80 | - Uri queryUri = MediaStore.Files.getContentUri("external"); | |
| 81 | - | |
| 82 | - CursorLoader cursorLoader = new CursorLoader( | |
| 83 | - mContext, | |
| 84 | - queryUri, | |
| 85 | - MEDIA_PROJECTION, | |
| 86 | - selection, | |
| 87 | - null, // Selection args (none). | |
| 88 | - MediaStore.Files.FileColumns.DATE_ADDED + " DESC" // Sort order. | |
| 89 | - ); | |
| 90 | - return cursorLoader; | |
| 62 | + queryUri = MediaStore.Files.getContentUri("external"); | |
| 91 | 63 | } |
| 92 | 64 | |
| 93 | - @Override | |
| 94 | - public void onLoadFinished(Loader loader, Object o) { | |
| 95 | - if (folders == null || mLoader == null || instance == null) return; | |
| 96 | - if (folders.size() > 0) { | |
| 97 | - mLoader.onData(folders); | |
| 98 | - return; | |
| 65 | + public ArrayList<Folder> load() { | |
| 66 | + if (folders == null || instance == null) return null; | |
| 67 | + if (!folders.isEmpty()) { | |
| 68 | + return folders; | |
| 99 | 69 | } |
| 100 | 70 | |
| 101 | 71 | Folder allFolder = new Folder(mContext.getResources().getString(R.string.all_dir_name)); |
| 102 | 72 | folders.add(allFolder); |
| 103 | 73 | Folder allVideoDir = new Folder(mContext.getResources().getString(R.string.video_dir_name)); |
| 104 | 74 | folders.add(allVideoDir); |
| 105 | - Cursor cursor = (Cursor) o; | |
| 75 | + Cursor cursor = mContext.getContentResolver().query(queryUri, MEDIA_PROJECTION, selection, null, MediaStore.Files.FileColumns.DATE_ADDED + " DESC"); | |
| 106 | 76 | if (cursor != null) { |
| 107 | 77 | while (cursor.moveToNext()) { |
| 108 | 78 | String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA)); |
| ... | ... | @@ -161,18 +131,10 @@ public class MediaLoader extends LoaderM implements LoaderManager.LoaderCallback |
| 161 | 131 | } |
| 162 | 132 | cursor.close(); |
| 163 | 133 | } |
| 164 | -// Log.e(TAG, "数据为空: " + folders.size()); | |
| 165 | - if (mLoader != null) mLoader.onData(folders); | |
| 166 | - } | |
| 167 | - | |
| 168 | - | |
| 169 | - @Override | |
| 170 | - public void onLoaderReset(Loader loader) { | |
| 171 | - | |
| 134 | + return folders; | |
| 172 | 135 | } |
| 173 | 136 | |
| 174 | - @Override | |
| 175 | - public void onDestory() { | |
| 137 | + public void destroy() { | |
| 176 | 138 | if (null != instance && null != folders) { |
| 177 | 139 | folders.clear(); |
| 178 | 140 | folders = null; | ... | ... |
lib_media/src/main/java/com/cnlive/media/media/loader/VideoLoader.java
| 1 | 1 | package com.cnlive.media.media.loader; |
| 2 | 2 | |
| 3 | -import android.app.LoaderManager; | |
| 4 | 3 | import android.content.Context; |
| 5 | -import android.content.CursorLoader; | |
| 6 | -import android.content.Loader; | |
| 7 | 4 | import android.database.Cursor; |
| 8 | 5 | import android.net.Uri; |
| 9 | -import android.os.Bundle; | |
| 10 | 6 | import android.provider.MediaStore; |
| 11 | 7 | import android.text.TextUtils; |
| 12 | -import android.util.Log; | |
| 13 | 8 | |
| 14 | - | |
| 15 | -import com.cnlive.media.media.DataCallback; | |
| 9 | +import com.cnlive.media.R; | |
| 16 | 10 | import com.cnlive.media.media.entity.Folder; |
| 17 | 11 | import com.cnlive.media.media.entity.Media; |
| 18 | -import com.cnlive.media.R; | |
| 19 | 12 | |
| 20 | 13 | import java.io.File; |
| 21 | 14 | import java.util.ArrayList; |
| ... | ... | @@ -25,10 +18,10 @@ import java.util.ArrayList; |
| 25 | 18 | * Created by dmcBig on 2017/6/9. |
| 26 | 19 | */ |
| 27 | 20 | |
| 28 | -public class VideoLoader extends LoaderM implements LoaderManager.LoaderCallbacks { | |
| 21 | +public class VideoLoader extends LoaderM { | |
| 29 | 22 | private final String TAG = this.getClass().getName(); |
| 30 | 23 | private static VideoLoader instance = null; |
| 31 | - private static ArrayList<Folder> folders=new ArrayList<>(); | |
| 24 | + private static ArrayList<Folder> folders = new ArrayList<>(); | |
| 32 | 25 | String[] MEDIA_PROJECTION = { |
| 33 | 26 | MediaStore.Files.FileColumns.DATA, |
| 34 | 27 | MediaStore.Files.FileColumns.DISPLAY_NAME, |
| ... | ... | @@ -41,53 +34,36 @@ public class VideoLoader extends LoaderM implements LoaderManager.LoaderCallback |
| 41 | 34 | MediaStore.Video.VideoColumns.DURATION}; |
| 42 | 35 | |
| 43 | 36 | Context mContext; |
| 44 | - DataCallback mLoader; | |
| 45 | 37 | |
| 46 | - public synchronized static VideoLoader getInstance(Context context, DataCallback loader) { | |
| 38 | + public synchronized static VideoLoader getInstance(Context context) { | |
| 47 | 39 | if (instance == null || folders == null) { |
| 48 | 40 | instance = new VideoLoader(context); |
| 49 | 41 | folders = new ArrayList<>(); |
| 50 | 42 | } |
| 51 | - instance.setmLoader(loader); | |
| 52 | 43 | return instance; |
| 53 | 44 | } |
| 54 | 45 | |
| 55 | 46 | public VideoLoader(Context context) { |
| 56 | 47 | this.mContext = context; |
| 48 | + selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "=" | |
| 49 | + + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO; | |
| 50 | + queryUri = MediaStore.Files.getContentUri("external"); | |
| 57 | 51 | } |
| 58 | 52 | |
| 59 | - public void setmLoader(DataCallback mLoader) { | |
| 60 | - this.mLoader = mLoader; | |
| 61 | - } | |
| 53 | + String selection; | |
| 54 | + Uri queryUri; | |
| 62 | 55 | |
| 63 | - @Override | |
| 64 | - public Loader onCreateLoader(int picker_type, Bundle bundle) { | |
| 65 | - String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "=" | |
| 66 | - + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO; | |
| 67 | 56 | |
| 68 | - Uri queryUri = MediaStore.Files.getContentUri("external"); | |
| 69 | - CursorLoader cursorLoader = new CursorLoader( | |
| 70 | - mContext, | |
| 71 | - queryUri, | |
| 72 | - MEDIA_PROJECTION, | |
| 73 | - selection, | |
| 74 | - null, // Selection args (none). | |
| 75 | - MediaStore.Files.FileColumns.DATE_ADDED + " DESC" // Sort order. | |
| 76 | - ); | |
| 77 | - return cursorLoader; | |
| 78 | - } | |
| 57 | + public ArrayList<Folder> load() { | |
| 58 | + if (folders == null || instance == null) return null; | |
| 59 | + if (!folders.isEmpty()) { | |
| 60 | + return folders; | |
| 79 | 61 | |
| 80 | - @Override | |
| 81 | - public void onLoadFinished(Loader loader, Object o) { | |
| 82 | - if (folders == null || mLoader == null || instance == null) return; | |
| 83 | - if (folders.size() > 0) { | |
| 84 | - mLoader.onData(folders); | |
| 85 | - return; | |
| 86 | 62 | } |
| 87 | 63 | |
| 88 | 64 | Folder allFolder = new Folder(mContext.getResources().getString(R.string.all_video)); |
| 89 | 65 | folders.add(allFolder); |
| 90 | - Cursor cursor = (Cursor) o; | |
| 66 | + Cursor cursor = mContext.getContentResolver().query(queryUri, MEDIA_PROJECTION, selection, null, MediaStore.Files.FileColumns.DATE_ADDED + " DESC"); | |
| 91 | 67 | while (cursor.moveToNext()) { |
| 92 | 68 | //获取的视频路径 |
| 93 | 69 | String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA)); |
| ... | ... | @@ -125,20 +101,12 @@ public class VideoLoader extends LoaderM implements LoaderManager.LoaderCallback |
| 125 | 101 | folders.add(folder); |
| 126 | 102 | } |
| 127 | 103 | } |
| 128 | -// Log.e(TAG, "数据为空: " + folders.size()); | |
| 129 | - | |
| 130 | - mLoader.onData(folders); | |
| 131 | 104 | cursor.close(); |
| 105 | + return folders; | |
| 132 | 106 | } |
| 133 | 107 | |
| 134 | 108 | |
| 135 | - @Override | |
| 136 | - public void onLoaderReset(Loader loader) { | |
| 137 | - | |
| 138 | - } | |
| 139 | - | |
| 140 | - @Override | |
| 141 | - public void onDestory() { | |
| 109 | + public void destroy() { | |
| 142 | 110 | if (null != instance && null != folders) { |
| 143 | 111 | folders.clear(); |
| 144 | 112 | folders = null; | ... | ... |
lib_media/src/main/java/com/cnlive/media/ui/activity/MediaActivity.java
| 1 | 1 | package com.cnlive.media.ui.activity; |
| 2 | 2 | |
| 3 | +import static com.cnlive.media.PickerConfig.REQUEST_CODE_PERMISSION_CAMERA; | |
| 4 | +import static com.cnlive.media.PickerConfig.REQUEST_CODE_PERMISSION_CROP; | |
| 5 | +import static com.cnlive.media.PickerConfig.REQUEST_CODE_PERMISSION_READ; | |
| 6 | + | |
| 3 | 7 | import android.Manifest; |
| 4 | 8 | import android.app.Dialog; |
| 5 | 9 | import android.content.Intent; |
| ... | ... | @@ -18,7 +22,6 @@ import com.cnlive.media.R; |
| 18 | 22 | import com.cnlive.media.databinding.ActivityMediaBinding; |
| 19 | 23 | import com.cnlive.media.frame.presenter.MediaPresenter; |
| 20 | 24 | import com.cnlive.media.frame.view.MediaView; |
| 21 | -import com.cnlive.media.media.DataCallback; | |
| 22 | 25 | import com.cnlive.media.media.entity.Folder; |
| 23 | 26 | import com.cnlive.media.media.entity.Media; |
| 24 | 27 | import com.cnlive.media.media.loader.ImageLoader; |
| ... | ... | @@ -44,9 +47,9 @@ import java.util.List; |
| 44 | 47 | import java.util.Observable; |
| 45 | 48 | import java.util.Observer; |
| 46 | 49 | |
| 47 | -import static com.cnlive.media.PickerConfig.REQUEST_CODE_PERMISSION_CAMERA; | |
| 48 | -import static com.cnlive.media.PickerConfig.REQUEST_CODE_PERMISSION_CROP; | |
| 49 | -import static com.cnlive.media.PickerConfig.REQUEST_CODE_PERMISSION_READ; | |
| 50 | +import io.reactivex.android.schedulers.AndroidSchedulers; | |
| 51 | +import io.reactivex.disposables.Disposable; | |
| 52 | +import io.reactivex.schedulers.Schedulers; | |
| 50 | 53 | |
| 51 | 54 | /** |
| 52 | 55 | * 作 者 : wkq |
| ... | ... | @@ -57,8 +60,7 @@ import static com.cnlive.media.PickerConfig.REQUEST_CODE_PERMISSION_READ; |
| 57 | 60 | * <p> |
| 58 | 61 | * 简 介 : |
| 59 | 62 | */ |
| 60 | -public class MediaActivity extends MvpBindingActivity<MediaView, MediaPresenter, Object, ActivityMediaBinding> implements DataCallback, Observer, View.OnClickListener { | |
| 61 | - | |
| 63 | +public class MediaActivity extends MvpBindingActivity<MediaView, MediaPresenter, Object, ActivityMediaBinding> implements Observer, View.OnClickListener { | |
| 62 | 64 | /** |
| 63 | 65 | * sdk23获取sd卡拍照权限的requestCode |
| 64 | 66 | */ |
| ... | ... | @@ -142,7 +144,6 @@ public class MediaActivity extends MvpBindingActivity<MediaView, MediaPresenter, |
| 142 | 144 | public static String[] permissionsRead = Build.VERSION.SDK_INT >= 33 ? |
| 143 | 145 | new String[]{ |
| 144 | 146 | "android.permission.READ_MEDIA_IMAGES", |
| 145 | - "android.permission.READ_MEDIA_AUDIO", | |
| 146 | 147 | "android.permission.READ_MEDIA_VIDEO"} : |
| 147 | 148 | new String[]{ |
| 148 | 149 | Manifest.permission.READ_EXTERNAL_STORAGE, |
| ... | ... | @@ -154,7 +155,10 @@ public class MediaActivity extends MvpBindingActivity<MediaView, MediaPresenter, |
| 154 | 155 | protected int getLayoutId() { |
| 155 | 156 | return R.layout.activity_media; |
| 156 | 157 | } |
| 158 | + | |
| 157 | 159 | long time; |
| 160 | + Disposable disposable; | |
| 161 | + | |
| 158 | 162 | @Override |
| 159 | 163 | protected void onCreate(Bundle savedInstanceState) { |
| 160 | 164 | super.onCreate(savedInstanceState); |
| ... | ... | @@ -163,7 +167,8 @@ public class MediaActivity extends MvpBindingActivity<MediaView, MediaPresenter, |
| 163 | 167 | if (getPresenter() != null) getPresenter().initIntentData(this); |
| 164 | 168 | if (getMvpView() != null) getMvpView().initView(); |
| 165 | 169 | getSupportFragmentManager().beginTransaction().add(R.id.frame, MediaPickerFragment.newInstanse(), MEDIA_HOME).addToBackStack("").commit(); |
| 166 | - time=System.currentTimeMillis(); | |
| 170 | + time = System.currentTimeMillis(); | |
| 171 | + binding.loading.setVisibility(View.VISIBLE); | |
| 167 | 172 | getMediaData(); |
| 168 | 173 | } |
| 169 | 174 | |
| ... | ... | @@ -172,24 +177,61 @@ public class MediaActivity extends MvpBindingActivity<MediaView, MediaPresenter, |
| 172 | 177 | .checkPermissions(this, permissionsRead, REQUEST_CODE_PERMISSION_READ, getResources().getString(R.string.dialog_imagepicker_permission_sdcard_nerver_ask_message), true); |
| 173 | 178 | |
| 174 | 179 | if (hasPermissions) { |
| 175 | - if (needCrop && singlePick) { | |
| 176 | -// ThumbnailsUtils.getThumbnails(this); | |
| 177 | - selectMode = PickerConfig.PICKER_IMAGE; | |
| 178 | - } | |
| 179 | - int type = selectMode; | |
| 180 | - if (type == PickerConfig.PICKER_IMAGE_VIDEO) { | |
| 181 | - loaderM = MediaLoader.getInstance(this, this); | |
| 182 | - getLoaderManager().initLoader(type, null, (MediaLoader) loaderM); | |
| 183 | - } else if (type == PickerConfig.PICKER_ONLY_ONE_TYPE) { | |
| 184 | - loaderM = MediaLoader.getInstance(this, this); | |
| 185 | - getLoaderManager().initLoader(type, null, (MediaLoader) loaderM); | |
| 186 | - } else if (type == PickerConfig.PICKER_IMAGE) { | |
| 187 | - loaderM = ImageLoader.getInstance(this, this); | |
| 188 | - getLoaderManager().initLoader(type, null, (ImageLoader) loaderM); | |
| 189 | - } else if (type == PickerConfig.PICKER_VIDEO) { | |
| 190 | - loaderM = VideoLoader.getInstance(this, this); | |
| 191 | - getLoaderManager().initLoader(type, null, (VideoLoader) loaderM); | |
| 192 | - } | |
| 180 | + disposable = io.reactivex.Observable.<ArrayList<Folder>>create(emitter -> { | |
| 181 | + ArrayList<Folder> list = null; | |
| 182 | + try { | |
| 183 | + if (needCrop && singlePick) { | |
| 184 | + selectMode = PickerConfig.PICKER_IMAGE; | |
| 185 | + } | |
| 186 | + int type = selectMode; | |
| 187 | + if (type == PickerConfig.PICKER_IMAGE_VIDEO) { | |
| 188 | + loaderM = MediaLoader.getInstance(this); | |
| 189 | + } else if (type == PickerConfig.PICKER_ONLY_ONE_TYPE) { | |
| 190 | + loaderM = MediaLoader.getInstance(this); | |
| 191 | + } else if (type == PickerConfig.PICKER_IMAGE) { | |
| 192 | + loaderM = ImageLoader.getInstance(this); | |
| 193 | + } else if (type == PickerConfig.PICKER_VIDEO) { | |
| 194 | + loaderM = VideoLoader.getInstance(this); | |
| 195 | + } | |
| 196 | + if (loaderM != null) { | |
| 197 | + // 快速读取缓存数据, 但是存在数据更新问题 | |
| 198 | + // list = new ArrayList<>(); | |
| 199 | + // for (Folder it : loaderM.load()) { | |
| 200 | + // Folder newFolder = new Folder(it.name); | |
| 201 | + // newFolder.count = it.count; | |
| 202 | + // it.getMedias().forEach(media -> { | |
| 203 | + // newFolder.addMedias(media.copy()); | |
| 204 | + // }); | |
| 205 | + // list.add(newFolder); | |
| 206 | + // } | |
| 207 | + | |
| 208 | + // 每次都加载新的数据, 但速度慢 | |
| 209 | + list = loaderM.load(); | |
| 210 | + } | |
| 211 | + } catch (Exception ignored) { | |
| 212 | + } | |
| 213 | + | |
| 214 | + if (list == null) { | |
| 215 | + list = new ArrayList<>(); | |
| 216 | + } | |
| 217 | + emitter.onNext(list); | |
| 218 | + }) | |
| 219 | + .subscribeOn(Schedulers.io()) | |
| 220 | + .observeOn(AndroidSchedulers.mainThread()) | |
| 221 | + .subscribe(list -> { | |
| 222 | + mediasList = list; | |
| 223 | + if (list == null || list.isEmpty()) return; | |
| 224 | + Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.frame); | |
| 225 | + if (currentFragment instanceof MediaPickerFragment) { | |
| 226 | + ((MediaPickerFragment) currentFragment).setMediaData(list); | |
| 227 | + Log.e("总数:", (list.toString()) + ""); | |
| 228 | + if (mFolderAdapter != null) mFolderAdapter.updateAdapter(list); | |
| 229 | + } | |
| 230 | + | |
| 231 | + ThumbnailsUtils.getThumbnails(MediaActivity.this); | |
| 232 | + | |
| 233 | + binding.loading.setVisibility(View.GONE); | |
| 234 | + }); | |
| 193 | 235 | } |
| 194 | 236 | } |
| 195 | 237 | |
| ... | ... | @@ -203,21 +245,6 @@ public class MediaActivity extends MvpBindingActivity<MediaView, MediaPresenter, |
| 203 | 245 | |
| 204 | 246 | |
| 205 | 247 | @Override |
| 206 | - public void onData(ArrayList<Folder> list) { | |
| 207 | - mediasList = list; | |
| 208 | - if (list == null || list.size() == 0) return; | |
| 209 | - Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.frame); | |
| 210 | - if (currentFragment instanceof MediaPickerFragment) { | |
| 211 | - ((MediaPickerFragment) currentFragment).setMediaData(list); | |
| 212 | - Log.e("时间:",System.currentTimeMillis()-time+""); | |
| 213 | - if (mFolderAdapter != null) mFolderAdapter.updateAdapter(list); | |
| 214 | - } | |
| 215 | - | |
| 216 | - ThumbnailsUtils.getThumbnails(this); | |
| 217 | - } | |
| 218 | - | |
| 219 | - | |
| 220 | - @Override | |
| 221 | 248 | protected void onDestroy() { |
| 222 | 249 | super.onDestroy(); |
| 223 | 250 | MediaAddObservable.getInstance().deleteObserver(this); |
| ... | ... | @@ -233,6 +260,12 @@ public class MediaActivity extends MvpBindingActivity<MediaView, MediaPresenter, |
| 233 | 260 | allMedias.clear(); |
| 234 | 261 | allMedias = null; |
| 235 | 262 | } |
| 263 | + if (disposable != null) { | |
| 264 | + disposable.dispose(); | |
| 265 | + } | |
| 266 | + if (loaderM != null) { | |
| 267 | + loaderM.destroy(); | |
| 268 | + } | |
| 236 | 269 | } |
| 237 | 270 | |
| 238 | 271 | @Override | ... | ... |
lib_media/src/main/res/layout/activity_media.xml
| 1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
| 2 | -<layout> | |
| 2 | +<layout xmlns:tools="http://schemas.android.com/tools"> | |
| 3 | 3 | |
| 4 | 4 | |
| 5 | 5 | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
| 6 | 6 | android:layout_width="match_parent" |
| 7 | - android:background="@color/color_defult_bg" | |
| 8 | - android:layout_height="match_parent"> | |
| 7 | + android:layout_height="match_parent" | |
| 8 | + android:background="@color/color_defult_bg"> | |
| 9 | + | |
| 9 | 10 | <include |
| 10 | 11 | android:id="@+id/top" |
| 11 | 12 | layout="@layout/meidia_picker_actionbar_top" /> |
| 12 | 13 | |
| 13 | 14 | <FrameLayout |
| 14 | - android:layout_below="@+id/top" | |
| 15 | - android:layout_above="@+id/footer" | |
| 16 | 15 | android:id="@+id/frame" |
| 17 | 16 | android:layout_width="match_parent" |
| 18 | - android:layout_height="match_parent" /> | |
| 17 | + android:layout_height="match_parent" | |
| 18 | + android:layout_above="@+id/footer" | |
| 19 | + android:layout_below="@+id/top" /> | |
| 20 | + | |
| 19 | 21 | <include |
| 20 | 22 | android:id="@+id/bottom" |
| 21 | 23 | layout="@layout/media_preview_bottom_bar" |
| 22 | 24 | android:visibility="gone" /> |
| 25 | + | |
| 23 | 26 | <include |
| 24 | 27 | android:id="@+id/footer" |
| 25 | 28 | layout="@layout/layout_main_footer" /> |
| 26 | 29 | |
| 30 | + <ProgressBar | |
| 31 | + android:id="@+id/loading" | |
| 32 | + style="@android:style/Widget.ProgressBar" | |
| 33 | + android:layout_width="30dp" | |
| 34 | + android:layout_height="30dp" | |
| 35 | + android:layout_centerInParent="true" | |
| 36 | + android:indeterminateDrawable="@drawable/drawable_pre_progress" | |
| 37 | + android:visibility="gone" | |
| 38 | + tools:visibility="visible" /> | |
| 27 | 39 | </RelativeLayout> |
| 28 | 40 | </layout> |
| 29 | 41 | \ No newline at end of file | ... | ... |