From 02d2d87a01fbaf9dde7807f43a5913aee808e7e8 Mon Sep 17 00:00:00 2001 From: Kelvin Lee Date: Mon, 1 Aug 2016 11:22:34 +0800 Subject: [PATCH] Fix load all image in Async Task instead of loading on UI thread --- .../MultiImageSelectorFragment.java | 109 +++++++++++------- 1 file changed, 70 insertions(+), 39 deletions(-) diff --git a/multi-image-selector/src/main/java/me/nereo/multi_image_selector/MultiImageSelectorFragment.java b/multi-image-selector/src/main/java/me/nereo/multi_image_selector/MultiImageSelectorFragment.java index 4c068c6..b7191f7 100644 --- a/multi-image-selector/src/main/java/me/nereo/multi_image_selector/MultiImageSelectorFragment.java +++ b/multi-image-selector/src/main/java/me/nereo/multi_image_selector/MultiImageSelectorFragment.java @@ -12,6 +12,7 @@ import android.graphics.Point; import android.graphics.drawable.ColorDrawable; import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.provider.MediaStore; @@ -426,49 +427,79 @@ private boolean fileExist(String path){ public void onLoadFinished(Loader loader, Cursor data) { if (data != null) { if (data.getCount() > 0) { - List images = new ArrayList<>(); - data.moveToFirst(); - do{ - String path = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[0])); - String name = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[1])); - long dateTime = data.getLong(data.getColumnIndexOrThrow(IMAGE_PROJECTION[2])); - if(!fileExist(path)){continue;} - Image image = null; - if (!TextUtils.isEmpty(name)) { - image = new Image(path, name, dateTime); - images.add(image); - } - if( !hasFolderGened ) { - // get all folder data - File folderFile = new File(path).getParentFile(); - if(folderFile != null && folderFile.exists()){ - String fp = folderFile.getAbsolutePath(); - Folder f = getFolderByPath(fp); - if(f == null){ - Folder folder = new Folder(); - folder.name = folderFile.getName(); - folder.path = fp; - folder.cover = image; - List imageList = new ArrayList<>(); - imageList.add(image); - folder.images = imageList; - mResultFolder.add(folder); - }else { - f.images.add(image); + new AsyncTask>() { + + List images = new ArrayList<>(); + int count = 0; + + @Override + protected List doInBackground(Cursor... datas) { + Cursor data = datas[0]; + data.moveToFirst(); + do { + String path = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[0])); + String name = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[1])); + long dateTime = data.getLong(data.getColumnIndexOrThrow(IMAGE_PROJECTION[2])); + if (!fileExist(path)) { + continue; } - } + Image image = null; + if (!TextUtils.isEmpty(name)) { + image = new Image(path, name, dateTime); + images.add(image); + } + if (!hasFolderGened) { + // get all folder data + File folderFile = new File(path).getParentFile(); + if (folderFile != null && folderFile.exists()) { + String fp = folderFile.getAbsolutePath(); + Folder f = getFolderByPath(fp); + if (f == null) { + Folder folder = new Folder(); + folder.name = folderFile.getName(); + folder.path = fp; + folder.cover = image; + List imageList = new ArrayList<>(); + imageList.add(image); + folder.images = imageList; + mResultFolder.add(folder); + } else { + f.images.add(image); + } + } + } + count++; + if (count % 100 == 0) { + publishProgress(count); + } + } while (data.moveToNext()); + return images; } - }while(data.moveToNext()); + @Override + protected void onProgressUpdate(Integer... values) { +// super.onProgressUpdate(values); + mImageAdapter.setData(images); + if (resultList != null && resultList.size() > 0) { + mImageAdapter.setDefaultSelected(resultList); + } + if (!hasFolderGened) { + mFolderAdapter.setData(mResultFolder); + } + } - mImageAdapter.setData(images); - if(resultList != null && resultList.size()>0){ - mImageAdapter.setDefaultSelected(resultList); - } - if(!hasFolderGened) { - mFolderAdapter.setData(mResultFolder); - hasFolderGened = true; - } + @Override + protected void onPostExecute(List images) { + mImageAdapter.setData(images); + if (resultList != null && resultList.size() > 0) { + mImageAdapter.setDefaultSelected(resultList); + } + if (!hasFolderGened) { + mFolderAdapter.setData(mResultFolder); + hasFolderGened = true; + } + } + }.execute(data); } } }