Reputation: 4044
I have an application - simple file manager, which show thumbnails for pictures and video using MediaStore.Video.Thumbnails.getThumbnail() and MediaStore.Images.Thumbnails.getThumbnail(). But it working slow in directories where many pictures. How to fix it?
Part of code:
private ArrayList<File> filesList = new ArrayList<File>();
// ...
class FilesListAdapter extends BaseAdapter {
// ...
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
File file = filesList.get(position);
Bitmap thumbnailBitmap = null;
ContentResolver crThumb = getContentResolver();
convertView = LayoutInflater.from(mContext).inflate(mResource, parent, false);
ImageView thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);
if (file.isDirectory()) {
thumbnail.setImageResource(R.drawable.efp__ic_folder);
} else {
if (Build.VERSION.SDK_INT >= 5) {
try {
if (Arrays.asList(videoExtensions).contains(getFileExtension(file.getName()))) {
Cursor cursor = crThumb.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Video.Media._ID }, MediaStore.Video.Media.DATA + "='" + file.getAbsolutePath() + "'", null, null);
if (cursor != null) {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
thumbnailBitmap = MediaStore.Video.Thumbnails.getThumbnail(crThumb, cursor.getInt(0), MediaStore.Video.Thumbnails.MICRO_KIND, null);
}
cursor.close();
}
} else if (Arrays.asList(imagesExtensions).contains(getFileExtension(file.getName()))) {
Cursor cursor = crThumb.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "='" + file.getAbsolutePath() + "'", null, null);
if (cursor != null) {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
thumbnailBitmap = MediaStore.Images.Thumbnails.getThumbnail(crThumb, cursor.getInt(0), MediaStore.Images.Thumbnails.MINI_KIND, null);
}
cursor.close();
}
}
} catch(Exception e) {
e.printStackTrace();
} catch (Error e) {
e.printStackTrace();
}
}
if (thumbnailBitmap == null) thumbnail.setImageResource(R.drawable.efp__ic_file);
else thumbnail.setImageBitmap(thumbnailBitmap);
}
// ...
return convertView;
}
}
Here is full code, if it needed: https://github.com/bartwell/ExFilePicker/blob/master/ExFilePicker/src/ru/bartwell/exfilepicker/ExFilePickerActivity.java
Upvotes: 0
Views: 725
Reputation: 10542
I used to struggle with Image loading trought out all my applications. Then Picasso came out.
I strongly suggest you to use this library if your app uses lots of bitmap. It will save you a ton of work
Upvotes: 1
Reputation: 61
you should read http://developer.android.com/training/displaying-bitmaps/index.html and especially the part about loading the images off the UI Thread.
You can grab and adapt a lot of code from the provided sample project (BitmapFun).
(I had the same issue before and using this strategy help me a lot ;-) )
Upvotes: 1