sorting images into gridview using date - android

I'm using the following code ,it picks image from gallery and displays image in random order
String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
String orderBy = MediaStore.Images.Media._ID;
imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
I want the images to get sorted via date it has been taken(Eg:Latest ones first and oldest photos later)...
and one more problem that when i use above code it only loads images in DCIM folder ,i want all the images from phone also.......

Change orderBy to
String orderBy = MediaStore.Images.Media.DATE_TAKEN + " DESC";

Related

How to load images from the phone taken in last hour?

I want to load and display all images that are taken by my phone in last hour. I am using Content Resolver query with some parameters to get images list. I can use this to display just last 10 images:
String sortOrder = String.format("%s limit 50 ", MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
And that is working with code from below, but I want to modify my cursor query to just display images taken in last hour.
Here is the code:
String orderBy = MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC LIMIT 10";
//Current time in millis and hour ago
int now = Calendar.getInstance().get(Calendar.MILLISECOND);
long hourAgo = now - 60*60000;
String[] columns = new String[]{
MediaStore.Images.Media._ID, // get the image
};
ContentResolver contentResolver = getContentResolver();
// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Images.Thumbnails._ID};
// Create the cursor pointing to the SDCard
cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
columns, // Which columns to return
null, // Return all rows
null,
orderBy);
// Get the column index of the Thumbnails Image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
How I can only get list of images taken in last hour?
I tried to write this also, but it does not work:
final String[] selectionArgs = { MediaStore.Images.ImageColumns.DATE_TAKEN + "<"+ Long.toString(hourAgo) };
cursor = contentResolver.query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
cols, // Which columns to return
null, // Return all rows
selectionArgs ,
orderBy);

How to make sort gallery thumbnails image by date

I am developing an android applicaiton. This application get all thumbnail images from gallery. I want to sort these thumbnails by date, but I can't do it.
Please help me.
Get all images
// Set up an array of the Thumbnail Image ID column we want
String[] columns = {MediaStore.Images.Media._ID};
String orderBy = MediaStore.Images.Thumbnails._ID + " DESC LIMIT 10";
// Create the cursor pointing to the SDCard
cursor = getActivity().managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
columns, // Which columns to return
null, // Return all rows
null,
orderBy);
// Get the column index of the Thumbnails Image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
myGalleryImages = (GridView) view.findViewById(R.id.my_gallery);
myGalleryImages.setAdapter(new ImageAdapter(getActivity()));
set images
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
holder.image.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
Update columns and orderBy like this:
String[] columns = {MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.DATE_TAKEN};
String orderBy = MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC";
and see if that helps.
You could also fetch real images instead of thumbnails and use image loading library that will take care of proper re-sizing. In this case replace your Thumbnails references with ImageColumns
this code will save the first 100 thumbs (SORTED by date)
Cursor cursor = this.getContentResolver().query( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
"image_id DESC");
// Get the column index of the Thumbnails Image ID
int columnIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA);
for(int i =0;i<cursor.getCount();i++){
if (i==100) break;
cursor.moveToPosition(i);
mImagesFromGallery[i] = cursor.getString(columnIndex);
}
cursor.close();

Contacts are not sorting in Android

I have inserted some raw contacts (given account type and name null). the native contact app of android shows all contacts are sorted ( merged previews and newly given). But in my app (a listview for displaying contacts), it shows first the previous contatcs (sorted by display name) and then newly inserted contacs (also sorted). I have tried whatever combination possible , but no luck. please help any one.
Query Code
String PROJECTION[] = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
private final String SORT_ORDER = ContactsContract.Contacts.DISPLAY_NAME + " ASC";
Uri uri = ContactsContract.Contacts.CONTENT_URI;
Cursor contacts = cr.query(uri, PROJECTION, null ,null, SORT_ORDER);
Update*strong text*
however , i was using handler , and now after converting to cusorloadr with loader manager. problem solved
Use getShort() method of Cursor to sort on the bases of a particular column.
try this as query :
Cursor cursor = getContentResolver.query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, null ,null, Phone.DISPLAY_NAME + " ASC");

Media Gallery ContentProvider

My app will display the complete list of images in my custom gallery .For this, I'm using ContentProvider of Image Thumbnails. Upon selecting the thumbnail's I need to display the actual image.According to my understanding Gallery's image do have same unique ID in Thumb and Media Table.
Here is the code. Firstly I queried Thumbnail's ContentProvider and saved URL and ID.
String pictureThumbTemp[] = { MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.DATA };
Cursor imagecursor = context.getContentResolver().query (MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
pictureThumbTemp,null, null, null);
Later I am displaying thumbs in Grid.
Upon selection of thumbnail, I have to display original image. I'm trying to retrieve the original image like
String pictureImageTemp[] = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
Cursor imagecursor = context.getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
pictureImageTemp, MediaStore.Images.Media._ID + " = " + mediaID + "", null,
MediaStore.Images.Media._ID);
Overall, I'm showing thumbnail through it's url and upon click I'm querying the thumbnail's media ID in Original image table.
But it is returning a cursor with 0 results.
Please help me out.
Thanks,
sha.
I cracked out a way which worked out.
Retrieved the cursor for Original Images.
From that I pulled the ID for every image and Queried the Thumbnails for the ID which returns a cursor containing paths.
Find the code snippet below.
String pictureCols[] = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
Cursor imagecursor = mContext.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, pictureCols,
null, null, null);
imagecursor.moveToFirst();
mImageUrls = new ArrayList<String>();
try {
// Iterate the cursor for Image urls
for (int index = 0; index < imagecursor.getCount(); index++) {
imagecursor.moveToPosition(index);
preparePicture(imagecursor);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
imagecursor.close();
}
}
Here is the code for preparePicture method
private void preparePicture(Cursor imageCursor) {
// get the ID for the original image
int idColumnIndex = imageCursor.getColumnIndex(mSelectedImage.mediaID);
Long id = imageCursor.getLong(idColumnIndex);
// Thumbnail image Cursor for this specific image.
String thumbCols[] = { MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.DATA };
Cursor thumbCursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(mContext.getContentResolver(), id,
Thumbnails.MINI_KIND, thumbCols);
thumbCursor.moveToFirst();
// Save thumbnail URL in MediaInfo
dataColumnIndex = thumbCursor.getColumnIndex(mSelectedThumb.data);
String thumbURL = thumbCursor.getString(dataColumnIndex);
thumbCursor.close();
mImageUrls.add(url);
}
Finally, I will have all my Thumbnail urls in the ArrayList.
The same logic is not working for video thumbnails. Of course that is a different question :)
Regards,
Sha.
try this one
final String[] pictureImageTemp= { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, pictureImageTemp, null,
null, MediaStore.Images.Media._ID);

sql where clause for managedQuery android method

I am trying to write the parameters to the managedQuery method in android and having trouble with the WHERE clause, here is what i wrote;
Cursor imageCursor;
// lots of other junk
String[] proj = {MediaStore.Images.Media.TITLE};
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, MediaStore.Images.Media.DATA = filename, proj, null );
"filename" is a string variable holding the path of an image, example /mnt/sdcard/pic05.png
I want it to return a cursor holding a record that is the same record for the pic05.png image and the cursor returned would hold the TITLE column information for this specific picture. How did i mess up the sql WHERE CLAUSE? I thought that my syntax must be wroing on the where clause
Add ' ' around filename. Try this
Cursor imageCursor;
// lots of other junk
String[] proj = {MediaStore.Images.Media.TITLE};
String selection = MediaStore.Images.Media.DATA + "='" + filename +"'";
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, selection, null, null );

Categories

Resources