Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix load all image in Async Task instead of loading on UI thread #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -426,49 +427,79 @@ private boolean fileExist(String path){
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (data != null) {
if (data.getCount() > 0) {
List<Image> 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<Image> imageList = new ArrayList<>();
imageList.add(image);
folder.images = imageList;
mResultFolder.add(folder);
}else {
f.images.add(image);
new AsyncTask<Cursor, Integer, List<Image>>() {

List<Image> images = new ArrayList<>();
int count = 0;

@Override
protected List<Image> 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<Image> 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<Image> images) {
mImageAdapter.setData(images);
if (resultList != null && resultList.size() > 0) {
mImageAdapter.setDefaultSelected(resultList);
}
if (!hasFolderGened) {
mFolderAdapter.setData(mResultFolder);
hasFolderGened = true;
}
}
}.execute(data);
}
}
}
Expand Down