There is a simple solution for this problem. First off, we need to implement Picasso pibrary, because, why do we invent wheel when it's already invented ? So this is how the getFileIcon method looks now:
public static void getFileIcon(File file, final ImageView icon, int numberOfFiles) {
if (Settings.showThumbnail() & isvalidMimeType(file)) {
Picasso.with(mContext).load(file).into(icon);
} else {
loadFromRes(file, icon, numberOfFiles);
}
}
here is improved version of loadFromRes() method, where we already know number of files so we don't count them again (We save a lot of memory):
private static void loadFromRes(final File file, final ImageView icon, final int numberOfFiles) {
Drawable mimeIcon = null;
if (file != null) {
if (file.isDirectory()) {
if (file.canRead() && numberOfFiles > 0) {
mimeIcon = mResources.getDrawable(R.drawable.type_folder);
} else {
mimeIcon = mResources.getDrawable(R.drawable.type_folder_empty);
}
} else if (file.isFile()) {
final String fileExt = SimpleUtils.getExtension(file.getName());
mimeIcon = mMimeTypeIconCache.get(fileExt);
if (mimeIcon == null) {
final int mimeIconId = MimeTypes.getIconForExt(fileExt);
if (mimeIconId != 0) {
mimeIcon = mResources.getDrawable(mimeIconId);
mMimeTypeIconCache.put(fileExt, mimeIcon);
}
}
}
}
if (mimeIcon != null) {
icon.setImageDrawable(mimeIcon);
} else {
// default icon
icon.setImageResource(R.drawable.type_unknown);
}
}
and finally, my solution to loading file information with icon, yeah as you already guessed, we need AsyncTask with a callback:
AsyncTask<File, Void, Integer> numberOfFiles = new CountFiles(new CountFiles.AsyncResponse() {
@Override
public void processFinish(int output) {
IconPreview.getFileIcon(file, mViewHolder.icon, output);
if (file.isFile()) {
// Shows the size of File
mViewHolder.bottomView.setText(SimpleUtils.formatCalculatedSize(file.length()));
} else {
// show the number of files in Folder
mViewHolder.bottomView.setText(output
+ mResources.getString(R.string.files));
}
}
}).execute(file);
There is a simple solution for this problem. First off, we need to implement Picasso pibrary, because, why do we invent wheel when it's already invented ? So this is how the getFileIcon method looks now:
here is improved version of loadFromRes() method, where we already know number of files so we don't count them again (We save a lot of memory):
and finally, my solution to loading file information with icon, yeah as you already guessed, we need AsyncTask with a callback:
and last piece of code:
that should be all, enjoy