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();
Related
please check image hereI need to set last taken image form gallery in image view or on Image button automatically just like used in default camera ,some body help me to sort out this problem
We can get the last taken image from MediaStore,
String[] proj = new String[]{
MediaStore.Images.ImageColumns.DATA,
};
final Cursor cursor = getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, null,
null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
if (cursor.moveToFirst()) {
String lastImageLocation = cursor.getString(0);
}
I am trying to populate recyclerview from gallery images as instagram imports gallery images while you select images from the list to upload. I want to implement similar type of thing. Any suggestions on how to access the gallery images on phone/memory card and populate the recyclerview. Also please do suggest any way to implement instagram like interface for selecting images to upload.
you can use Android media store to pick pictures,Videos and Audios
example code for images:
private void imageMediaQuery() {
String[] projection = new String[]{
MediaStore.MediaColumns.DATA,
//MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN
};
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String BUCKET_GROUP_BY =
"1) GROUP BY 1,(1";
String BUCKET_ORDER_BY = MediaStore.Images.Media.DATE_TAKEN + " DESC";
Cursor cur = this.getContentResolver().query(images,
projection, // Which columns to return
BUCKET_GROUP_BY, // Which rows to return (all rows)
null, // Selection arguments (none)
BUCKET_ORDER_BY // Ordering
);
if(cur!=null){
Log.i("ListingImages", " query count=" + cur.getCount());
}
if (cur!=null&&cur.moveToFirst()) {
String bucket;
String path;
int bucketColumn = cur.getColumnIndex(
MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
int pathColumn = cur.getColumnIndex(
MediaStore.MediaColumns.DATA);
do {
bucket = cur.getString(bucketColumn);
path = cur.getString(pathColumn);
} while (cur.moveToNext());
cur.close();
}
}
In the above code you get path for each image and simply use that path in your onBindViewHolder of recyclerview adapter to display images.hope this helps.
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);
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";
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);