I've developed an android app that shows in an adaptor all photos the user has in its device.
This is the code I'm using to read the photos:
Cursor photos;
String pictureCols[] = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
String order = Media.DATE_ADDED + " DESC";
photos = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, pictureCols, null, null, order);
photos.moveToFirst();
photosAdapter.addItems(photos);
And inside the adapter this is the code I use to show them:
public View getView(final int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(R.layout.photo_item, null);
}
else
{
gridView = (View) convertView;
}
photoIV = (SquareImageView)gridView.findViewById(R.id.photo);
long photoId = photos.getLong(0);
String thumbURL = "file://"+photos.getString(1);
String thumbCols[] = { MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.DATA };
Cursor thumbCursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(context.getContentResolver(), photoId, Thumbnails.MINI_KIND, thumbCols);
if(thumbCursor.getCount() > 0) {
thumbCursor.moveToFirst();
thumbURL = "file://"+thumbCursor.getString(1);
}
thumbCursor.close();
if(!thumbURL.equals(""))
{
mImageOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.resetViewBeforeLoading(true)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.build();
ImageLoader.getInstance().displayImage(thumbURL, photoIV, mImageOptions);
}
return gridView;
}
This code is working OK on all devices I've tested so far but some users with Samsung Galaxy S7 have reported that most of their photos are shown rotated. I cannot find any documentation related to this issue in S7. Has S7 changed something related to this? Should I read the photos in a different way to get their correct orientation? This code has been working for a long time in a lot of devices.
If you use an image-loading library, and you care about EXIF orientation headers, choose an image-loading library that applies those headers for you (e.g., Picasso, at least for local images). This may require some sort of an opt-in call (e.g., considerExifParams(true), per your comment).
Not all images will have an EXIF orientation header. It depends on whether it is a photo, what sort of camera hardware took the photo, whether the camera app that took the photo rotated the image for you, etc.
Related
I am trying to implement a feature like Instagram or WhatsApp, where the thumbnail of a single image that exists in a folder in android, is shown on top of a list item, more like a sample of what kinds of image are in the folder.
Help me to understand this feature.
How I implemented it. It might not be the best though, but it works.
I fetched the URIs of all the images using MediaStore, you can learn how to use it here.
The First step was done in a background thread to prevent it from blocking the UI thread.
I sorted out the images I got, grouping them in a List<Image>, which would represent a single directory.
I then added the List<Image> into a List<List<Image>>, which served as the overall images that were fetched and have their total size which I used later to track the number of images in the directory.
The code is below.
#Override
public void run() {
Uri storageUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
storageUri = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
} else {
storageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
// the queries to the MediaStore API (The image details or metadata I need
String[] projection = {
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.SIZE,
MediaStore.Images.Media.DISPLAY_NAME};
// now query the MediaStore API using ContentResolver
Cursor imgCursor = getApplicationContext().getContentResolver().query(storageUri, projection, null, null, null);
int bucketId = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
int imgSize = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE);
int name = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
int bucketName = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
// directoryDictionary is a temporary list of directory names that was found, while querying the MediaStore API
List<String> directoryDictionary = new ArrayList<>();
// generalList is just a list that would represent a general image list, where all images can be found. Just like Whatsapp
List<Image> generalList = new ArrayList<>();
while (imgCursor.moveToNext()) {
long id = imgCursor.getLong(bucketId);
int size = imgCursor.getInt(imgSize);
String fileName = imgCursor.getString(name);
String folderName = imgCursor.getString(bucketName);
// As recommended by the Android developers doc
Uri contentUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
// a single image
Image currentImage = new Image(contentUri, size, fileName, folderName);
// add all images to the general image list, but modifying the directory name
Image genImage = new Image(contentUri, size, fileName, "All Media");
generalList.add(genImage);
int directoryIndex = CollectionUtils.linearSearch(directoryDictionary, folderName);
// if search result (directoryIndex) passes this test, then it means that there is
// no such directory in list of directory names
if (directoryIndex < 0) {
imageDirectoryList.add(new ArrayList<>());
directoryDictionary.add(folderName);
directoryIndex = CollectionUtils.linearSearch(directoryDictionary, folderName);
if (directoryIndex >= 0)
imageDirectoryList.get(directoryIndex).add(currentImage);
} else {
imageDirectoryList.get(directoryIndex).add(currentImage);
}
}
//...then add it if the image list of folder is > 2
if (imageDirectoryList.size() > 2) imageDirectoryList.add(0, generalList);
imgCursor.close();
runOnUiThread(() -> {
// imageAdapter is the RecyclerView's list Adapter.
// notifyDataSetChanged() must be call to refresh list.
imageAdapter.notifyDataSetChanged();
// doViewUpdate was just used to turn on and off the visibility of some views
doViewUpdate();
});
}
I'm fairly new to android programming. I've run into some issues loading images (bitmaps) into a ListView. It works flawlessly until I have around 5 images loaded, then it reuses the first image I loaded (assuming from cache). I've tried a lot of solutions and bitmap samplings but, still no luck. Here is my code:
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);
Contact currentContact = Contacts.get(position);
TextView name = (TextView) view.findViewById(R.id.contactName);
name.setText(currentContact.getName());
TextView phone = (TextView) view.findViewById(R.id.phoneNumber);
phone.setText(currentContact.getPhone());
TextView email = (TextView) view.findViewById(R.id.email);
email.setText(currentContact.getEmail());
TextView address = (TextView) view.findViewById(R.id.cAddress);
address.setText(currentContact.getAddress());
Context mContext = getApplicationContext();
File file = mContext.getDir("imageDir", Context.MODE_PRIVATE);
String path = "file:" + file.getPath() + "/" + currentContact.getImage() + ".png";
ImageView contactListImage = (ImageView) view.findViewById(R.id.contactListImage);
Picasso.with(getApplicationContext())
.load(path)
.centerInside()
.fit()
.error(R.drawable.user_2)
.into(contactListImage)
}
return view;
}
I did try using
recycleMemoryCache()
Thank in you advance!
You're not using the recycled view correctly.
The view parameter you have there is either null, which means you have to inflate a new one, or not null, which means you should re-use it - assign new values to all the fields etc.
You simply need to move the closing curly-brace above return view; to just after your view inflation:
if (view == null) {
view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);
}
As an aside, I'd look up the viewholder pattern - or just start using RecyclerView, as that enforces it (and is the way of the future for lists on Android).
I'm trying to show a list of contacts along with the photo associated to them, but I see most of the contacts without the photo although they do have a photo when I go to the phone's (SG2) default contact list. I figured that this is because some of those contacts are actually merged, and the picture comes from the (raw?) contact that is not displayed in the list, so the question is - how do I get all the contacts merged with a given contact? I tried to play with the SQLite, but I can't find any column that will uniquely identify merged contacts.
From code perspective, I started with this method:
protected Bitmap getImageByContactId(final Context appContext, final String strContactId)
{
Bitmap bmContactImage = null;
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(strContactId));
Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
InputStream image = null;
try {
image = appContext.getContentResolver().openInputStream(photoUri);
bmContactImage = BitmapFactory.decodeStream(image);
try {
image.close();
} catch (IOException e ) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// This can be ignored because the Uri is generic and photo may not really exist
}
return bmContactImage;
}
but this only returned photos associated to the "original" contact that matched the original search criteria (in my case - the contact id) - not if the picture was associated to a contact linked to the original contact.
I was hoping to make something like the following work, if I could only get the (raw?) id of the contacts linked/associated/merged with the original contact, but I can't seem to find a way to find those "other" contacts...
public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
Uri uri = Uri.parse(String.format("content://com.android.contacts/contacts/%s", id));
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
if (input == null) {
return null;
}
return BitmapFactory.decodeStream(input);
}
Bottom line - what I'm trying to do is to show a photo of a contact, regardless whether it is from the contact matching the search criteria - or any of the contacts merged with that contacts - and any solution will be appreciated!
Thanks!
(I got here from our facebook page..)
here is how I get all contacts from my content resolver:
public void addContactsMultiple()
{
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
String id="";//Contact ID
String name="";//Contact Name
id = "";
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(Integer.parseInt
(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//Getting the Contact Image
if(cur.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)) != null)
{
imagePath = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
}
else {imagePath="none";}//If there is no contact image
}
}
cur.close();
}
In my code you get the image URI and than put it in your Contact object, than you can use
ImageView image = (ImageView)findViewById(R.id.image)
image.setImageURI(Uri.parse(imagePath));
or you can use Picasso Image Loader:
Picasso
and than use this code:
Picasso.with(context)
.load(imagePath)
.skipMemoryCache()
.into(image );
As You can see, I don't use BitMaps or decodeStream.
You only need the uri of the photo (which directs your image to the Android gallery)
if you get several contacts from the same name you can use this code to marge them:
//(mContactList) is the ArrayList of contacts object I have created)
HashSet<Contact> hs = new HashSet<Contact>();
hs.addAll(mContactList);
mContactList.clear();
mContactList.addAll(hs);
this code will delete duplications
with a little adjustments you can get the image uri to the contact it belongs..
come back if you have more questions.
Gist: custom adapter gets file resource indirectly via filepath in database. Inefficiency / memory concerns. Your opinion requested.
References to all searches, related links, and things useful re topic are at post bottom.
Code below works, but several factors are of concern. Need some more experienced eyes on this please to suggest improvement or potential errors to avoid. App doesn't need to be a content provider (data sourced local to app only). The ListView in question will be very light weight with only about 5 to max 10 entries. (I left out the database stuff because it works.)
Overview:
DataBase contains some text and an Image File path. - OK
image files are stored on device (SD card / external storage, where ever). - OK
That the files are not in the database makes this different than a normal SimpleCursorAdapter - have to pull the image file. Added overhead of making it into a thumbnail before populating the listview.
As said, it's light, however, even with only one or two entries, the VM is burping. I suspect it's all the memory joggling related to the Bitmaps:
08-27 19:53:14.273: I/dalvikvm-heap(11900): Grow heap (frag case) to 4.075MB for 1228816-byte allocation
08-27 19:53:14.393: D/dalvikvm(11900): GC_CONCURRENT freed <1K, 5% free 4032K/4244K, paused 13ms+3ms, total 116ms
/* myTextAndImageCursorAdapter.java */
import android.widget.SimpleCursorAdapter;
//import android.support.v4.widget.SimpleCursorAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.database.Cursor;
import java.io.File;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import static android.media.ThumbnailUtils.extractThumbnail;
public class TextAndImageCursorAdapter extends SimpleCursorAdapter {
private Context context;
private int layout;
public TextAndImageCursorAdapter (Context ctx, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = ctx;
this.layout = layout;
}
#Override
public View newView(Context ctx, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(ctx);
View vView = inflater.inflate(layout, parent, false);
int iCol_Text = c.getColumnIndex(DBCOL_TEXT);
int iCol_Image = c.getColumnIndex(DBCOL_IMAGE);
String sText = c.getString(iCol_Text);
String sFileAndPath_Image = c.getString (iCol_Image); //// sImage path & file
TextView tvText = (TextView) v.findViewById(R.id.gui_text);
if (tvText != null) {
tvText.setText(sSomeText);
}
ImageView ivImage (ImageView) v.findViewById(R.id.gui_image);
if (ivImage != null) {
ivImage.setImage (mySetImage (sFileAndPath_Image) );
}
return vView;
}
#Override
public void bindView(View v, Context ctx, Cursor c) {
//// ( like newView(), without an inflater, view, or return )
int iCol_Text = c.getColumnIndex(DBCOL_TEXT);
int iCol_Image = c.getColumnIndex(DBCOL_IMAGE);
String sText = c.getString(iCol_Text);
String sFileAndPath_Image = c.getString (iCol_Image); //// path & file
TextView tvText = (TextView) v.findViewById(R.id.gui_text);
if (tvText != null) {
tvText.setText(sSomeText);
}
ImageView ivImage (ImageView) v.findViewById(R.id.gui_image);
if (ivImage != null) {
ivImage.setImageBitmap ( mySetImage ( sFileAndPath_Image ) ) ;
}
}
/////
/////
protected Bitmap mySetImage ( String path ) {
int width = 60; int height = 40 ;
File imgFile = new File ( path ); //// usually like: \sdcard0\wherever\filename1234.bmp
Bitmap myBitmap = null;
if( imgFile.exists() )
{
myBitmap = BitmapFactory.decodeFile ( imgFile.getAbsolutePath () );
}
else
Log.d ("oops", "no image file ... using default.");
myBitmap = getTheDefaultImage (); //// not shown - this is arbitrary
}
imgFile.close();
return ( extractThumbnail ( myBitmap, width, height ) ) ;
}
}
[EDIT - added links]
Search Criteria:
"
Android custom simplecursoradapter with image from file with path in database
"
The nearest hit, but attempts to pull image from res, not from external / sd store (unanswered):
SimpleCursorAdapter how to show an image?
Also a near hit / miss - similar algo (unanswered) references 4):
Customizing list shown from SimpleCursorAdapter using ViewBinder
Almost, but OP's code doesn't work, (no working answer):
Load Image in a custom list by SimpleCursorAdapter
Working for OP, but uses JSON for remote retrieval, not local (maybe this could be tweaked, but it's not clear to me how).
How to show images in imageview in simple adapter?
Not quite, but again close:
ListView scroll slow while loading image from Internal Storage
Image Loader problems (references 2):
Imageloader not loading image on real device
Related links:
Android Custom Cursor Adapter
Android: Issue with newView and bindView in custom SimpleCursorAdapter
Similarly named hits, but unrelated to my specific questions - these usually refer to in-app RESources:
show image from database where you saved the path of image
Custom SimpleCursorAdapter error
Custom SimpleCursorAdapter, database query and NullPointerException
nullPointerException with extended SimpleCursorAdapter
Android SimpleCursorAdapter - Adding conditional images
External References:
0) Simple intro tut on custom cursor adapters
http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/
1) Romain Guy - basic layout ... 2 txts, 1 image
http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/
2) AQuery (Android Query)
http://code.google.com/p/android-query/wiki/ImageLoading
3) Android thumbnails
http://developer.android.com/reference/android/media/ThumbnailUtils.html
4) Cust. listview with an "on/off" star image:
http://enjoyandroid.wordpress.com/2012/03/12/customizing-simple-cursor-adapter/
Two things you can do:
1) Use the ViewHolder pattern, cache the LayoutInfalter and most important: don't bind data twice:
/* ... imports */
import static android.media.ThumbnailUtils.extractThumbnail;
public class TextAndImageCursorAdapter extends SimpleCursorAdapter {
private LayoutInflater mLayoutInflater;
private Context context;
private int layout;
private class ViewHolder {
TextView textView;
ImageView imageView;
ViewHolder(View v) {
textView = (TextView) v.findViewById(R.id.gui_text);
imageView = (ImageView) v.findViewById(R.id.gui_image);
}
}
public TextAndImageCursorAdapter (Context ctx, int layout, Cursor c, String[] from, int[] to) {
super(ctx, layout, c, from, to);
this.context = ctx;
this.layout = layout;
mLayoutInflater = LayoutInflater.from(ctx);
}
#Override
public View newView(Context ctx, Cursor cursor, ViewGroup parent) {
View vView = mLayoutInflater.inflate(layout, parent, false);
vView.setTag( new ViewHolder(vView) );
// no need to bind data here. you do in later
return vView;// **EDITED:**need to return the view
}
#Override
public void bindView(View v, Context ctx, Cursor c) {
// you might want to cache these too
int iCol_Text = c.getColumnIndex(DBCOL_TEXT);
int iCol_Image = c.getColumnIndex(DBCOL_IMAGE);
String sText = c.getString(iCol_Text);
String sFileAndPath_Image = c.getString (iCol_Image); //// path & file
ViewHolder vh = (ViewHolder) v.getTag();
vh.textView.setText(sSomeText);
vh.imageView.setImageBitmap ( mySetImage ( sFileAndPath_Image ) );
}
}
2) This is really important: don't create a thumbnail on every bind. you need to cache the result:
private void setThumbnail(String path, Bitmap b) {
// save thumbnail to some kind of cache
// see comment below
}
private Bitmap getThumbnail(String path) {
Bitmap thumbnail = null;
// try to fetch the thumbnail from some kind of cache
// see comment below
return thumbnail;
}
protected Bitmap mySetImage ( String path ) {
int width = 60; int height = 40 ;
Bitmap thumbnail = getThumbnail(path); // try to fetch thumbnail
if (thumbnail != null) return thumbnail;
File imgFile = new File ( path ); //// usually like: /sdcard/wherever/filename1234.bmp
Bitmap myBitmap = null;
if( imgFile.exists() ) {
myBitmap = BitmapFactory.decodeFile ( imgFile.getAbsolutePath () );
} else {
Log.d ("oops", "no image file ... using default.");
myBitmap = getTheDefaultImage (); //// not shown - this is arbitrary
}
imgFile.close();
thumbnail = extractThumbnail ( myBitmap, width, height );
myBitmap.recycle();
setThumbnail(path, thumbnail); // save thumbnail for later reuse
return thumbnail;
}
Depending on you use case, you want to fill getThumbnail() and setThumbnail() with some kind of LruCache:
There is a in memory LruCache available in the android API and in support lib: https://developer.android.com/reference/android/util/LruCache.html
Jake made an persistent DiskLruCache: https://github.com/JakeWharton/DiskLruCache
EDIT :
#Override
public View newView(Context ctx, Cursor cursor, ViewGroup parent) {
View vView = mLayoutInflater.inflate(layout, parent, false);
vView.setTag( new ViewHolder(vView) );
// no need to bind data here. you do in later
return vView;// **EDITED:**need to return the view
}
Hi all,
I have searched a lot for my problem, I found a lot of posts with similar problems, but no one gave me a correct solution.
What I want is a gridview displaying a sdcard folder's images. I also have to offer the possibility to take a picture, and when going back to the gridview, update it with the new picture.
To take the picture, I use this code :
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageFileUri());
startActivityForResult(intent, TAKE_PICTURE_ACTIVITY);
Where getImageFileUri() is a function giving me a picture name with a timestamp, using Environment.getExternalStorageDirectory() to get the sdcard path, and checking if the folder exists (and create it if it doesn't).
For the moment, I use a cursor to get my images :
private void displayGallery() {
// Query params :
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Images.Media._ID};
String selection = MediaStore.Images.Media.DATA + " like ? ";
String[] selectionArgs = {"%Otiama%"};
// Submit the query :
mCursor = managedQuery(uri, projection, selection, selectionArgs, null);
if (mCursor != null) {
mGridView.setOnItemClickListener(this);
mGridView.setAdapter(new ImageAdapter(this, mCursor));
}
else showToast("Gallery is empty : " + uri.toString());
}
And here is my adapter's getView :
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
// Move cursor to current position
mCursor.moveToPosition(position);
// Get the current value for the requested column
int columnIndex = mCursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
int imageID = mCursor.getInt(columnIndex);
// obtain the image URI
Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
String url = uri.toString();
// Set the content of the image based on the image URI
int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(), originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
imageView.setImageBitmap(b);
return imageView;
}
This code works, but is very slow and doesn't update (new pictures's thumbnails aren't created), even if I call again my displayGallery() function in the onActivityResult(). Well, it doesn't update even if I reload the app >< . I have to run it again from eclipse.
In fact, what I would like is the same behavior than ES File Explorer (when you open a folder, the pictures have all a preview image, and they are loaded asynchronously), which, I think, doesn't use those ** thumbnails.
So I tried to load the pictures as bitmaps using the bitmap factory, but even with a few pictures (1-2), I instantly get a "java.lang.OutOfMemoryError: bitmap size exceeds VM budget"... I guess I have to resize them, but if I do so, won't I have the same error when I will load 20-30 pictures ?? Or the problem is that each picture exceeds the budget, and so if I resize them I will avoid this error for all of them ?
Sorry for the big post, if someone can help...
Well, I answer myself :
I created my own thumbnails this way :
public static Bitmap resizedBitmap(Bitmap bitmap, int newWidth) {
// Get the bitmap size :
int width = bitmap.getWidth();
int height = bitmap.getHeight();
double ratio = (double)width / (double)height;
// Compute the thumbnail size :
int thumbnailHeight = newWidth;
int thumbnailWidth = (int) ( (double)thumbnailHeight * ratio);
// Create a scaled bitmap :
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, thumbnailWidth, thumbnailHeight, false);
return scaledBitmap;
}
I don't use cursors anymore, to load the thumbnails I proceed like this (in the ImageAdapter) :
public void loadThumbnails() {
// Init the ArrayList :
_thumbnails = new ArrayList<ImageView>();
_imagesNames = new ArrayList<String>();
// Run through the thumbnails dir :
File imagesThumbnailsDir = new File(_imagesThumbnailsDirUri.getPath());
File[] imagesThumbnails = imagesThumbnailsDir.listFiles();
Arrays.sort(imagesThumbnails);
// For each thumbnail :
for(File imageThumbnail : imagesThumbnails)
{
// Check if the image exists :
File image = new File(_imagesDirUri.getPath() + File.separator + imageThumbnail.getName());
if(image.exists()) {
ImageView imageView = new ImageView(_context);
imageView.setImageDrawable(Drawable.createFromPath(imageThumbnail.getAbsolutePath()));
_thumbnails.add(imageView);
_imagesNames.add(imageThumbnail.getName());
}
// If not, delete the thumbnail :
else {
ImageUtils.deleteFile(Uri.fromFile(imageThumbnail));
}
}
}
And so my ImageAdapter's getView function sounds like this :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(_context);
imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageDrawable(_thumbnails.get(position).getDrawable());
return imageView;
}
Hope it helps..