Load images depending on creation date - android

I'm searching a way to find and load images from Pictures folder depending on creation date. For example I want to load all pictures took in the last month, but I don't know their name.
I already read about Picasso library, but as written in the documentation, the url is required.
So instead of scan the entire folder and then check creation date, does it exist a quicker way to accomplish this task?
Thanks

Maybe this could work for you. You may add selection clauses to query as well, to filter files by date for example.
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = getContentResolver().query(uri, new String[] {MediaStore.Images.Media.DATA}, null, null, MediaStore.Images.Media.DATE_ADDED + " ASC");
if (cursor != null) {
while (cursor.moveToNext()) {
Uri imageUri = Uri.parse(cursor.getString(0));
}
cursor.close();
}

Use Timestamp while storing Images
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
Give this as file name. This works only if you use separate activity for clicking images.

Related

Android Content Uri Format

I have been retrieving images from MediaStore in the following way...
Uri uriExternal = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {
MediaStore.MediaColumns._ID,
MediaStore.MediaColumns.DATE_ADDED
};
Cursor cursor = getContentResolver()
.query(uriExternal, projection,
MediaStore.MediaColumns.DATA + " IS NOT NULL",
null,
MediaStore.MediaColumns.DATE_ADDED + " DESC");
if(cursor != null) {
while (cursor.moveToNext()) {
String _id = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
paths.add(uriExternal.toString() + "/" + _id);
}
cursor.close();
}
Basically, I'm simply appending the file id to the external content provider uri. This makes a uri that I can use with content providers...
content://media/external/images/media/{id}
It all works perfectly fine, all external images are displayed and loaded flawlessly. However, since I've failed to find proper documentation, I'm a little concerned I'm not doing things the proper way. Especially because of the way I'm constructing the uri...kind of hard-coding it...
The questions are...
Is this the correct way to construct a content uri for an external image?
Is there a more reliable way to achieve this?
Personally, I use ContentUris.withAppendedId(). That way, I don't have to worry about whether I am starting with a Uri that ends in / or not. :-)
In general, MediaStore adheres to the original ContentProvider vision of using the content ID as the last path segment of a Uri pointing to the content. However, that is not a general rule, and it will not work for all providers.

Apache FileUtils.MoveFile does not delete image after its been moved, device restart is required for the update to take place

I'm using org.apache.commons.io.FileUtils to move files from one directory to another, like so:
File source = new File("/my/image/directory/image.jpg";
File destination = new File("/new/image/directory/image.jpg");
FileUtils.moveFile(source, destination);
Note that the source directory is an external directory and destination is an internal directory.
After above code is executed, I query to get the files of my internal directory like so:
File vaultDir = ctx.getDir("dir_name", Context.MODE_PRIVATE);
String[] fileList = vaultDir.list();
//Iterate and print file names
This is working as intended. But when i query the MediaStore like so:
ArrayList<GridViewObject> objects = new ArrayList<>();
String[] mProjection = {MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.DATA};
ContentResolver cr = ctx.getContentResolver();
Cursor mCursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
mProjection,
null,
null,
MediaStore.Images.Media.DEFAULT_SORT_ORDER);
mCursor.moveToFirst();
while (!mCursor.isAfterLast()) {
GridViewObject tmpGridViewObject = new GridViewObject();
tmpGridViewObject.setTitle(mCursor.getString(0));
tmpGridViewObject.setUrl(mCursor.getString(1));
objects.add(tmpGridViewObject);
mCursor.moveToNext();
}
The image is still returned to me by the query. Also, when I open my camera gallery app, the image is still there, as if it was never deleted.
I noticed that if I restart the phone, the image disappears from my device camera gallery app as intended (and above query return correct result). So I think it might be an indexing problem.
What do I need to do to "update" the MediaStore to show the intended result?
I solved this by instead of deleting the file using FileUtils.delete(srcFile, destFile); i copied the the file using FileUtils.copy(srcFile, destFile); and then to refresh MediaStore i used the following code to delete the source entry:
String selection = MediaStore.Images.Media.DATA + "='" + source.getAbsolutePath() + "'";
ctx.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, null);
Credit goes to Pascal's answer to this question.

How to get path of thumbnail from mediastore in reasonable time to load images in a gridview?

I'm trying to show thumbnails of images from sdcard in a gridview. I'm using universal image loader and image loading is takes reasonable time.
My problem is: trying to get all paths for the thumbnails takes too much time and causes lag on the gridview scroll. If i try ro read all paths ahead of the loading images then it takes 10 seconds (500 images on S5). I see a lot of app already solved this problem and it seems i am making some fundemantal mistakes here, yet i couldn't find what it is.
PS: I got this code from an answer on stackoverflow and i think my problem is not related to code but it is related with my approach to the problem.
Here is the code:
public static String getThumbnailPath(Activity activity, long imageId) {
String[] proj = { MediaStore.Images.Media.DATA };
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
CursorLoader cursorLoader = new CursorLoader(activity, uri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
cursor.moveToFirst();
String result="";
cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(activity.getContentResolver(), imageId,
MediaStore.Images.Thumbnails.MINI_KIND, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
result = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
cursor.close();
}
result = "file://" + result;
return result;
}
Have a look at smoothie by Lucas Rocha. It is a library project for fast scrolling listviews and gridviews. I use it in my app Playlist Manager. Easy to implement.

Image Thumbnail Refresh in Gallery

For my application, I need to refresh thumbnail image of gallery.
Because whenever I update my image content that thing don't reflected on thumbnail. The thumbnail image show first time image creation data not the present one.
But actual image contain new data so I need to reflect it into thumbnail also.
After wandering this forum I found following code snipper to delete current thumbnail but I can't able to call this function because I don't able to get photoId.
private static void removeThumbnails(ContentResolver contentResolver, long photoId) {
Cursor thumbnails = contentResolver.query(Thumbnails.EXTERNAL_CONTENT_URI, null, Thumbnails.IMAGE_ID + "=?", new String[]{String.valueOf(photoId)}, null);
for (thumbnails.moveToFirst(); !thumbnails.isAfterLast(); thumbnails.moveToNext()) {
long thumbnailId = thumbnails.getLong(thumbnails.getColumnIndex(Thumbnails._ID));
String path = thumbnails.getString(thumbnails.getColumnIndex(Thumbnails.DATA));
File file = new File(path);
if (file.delete()) {
contentResolver.delete(Thumbnails.EXTERNAL_CONTENT_URI, Thumbnails._ID + "=?", new String[]{String.valueOf(thumbnailId)});
}
}
At present I was calling this function as
removeThumbnails(pexelMimics.getContentResolver(),
ContentUris.parseId(uri));
I think this trick will work for me. But this is my guess. If you guys have better solution for this problem then please help me.
At the time of image saving, I have to provide following code to delete image content.
activity.getContentResolver().delete(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaColumns.DATA
+ "='"
+ GameManager.getInstance().getSavedImageFilePath()
.replaceAll("'", "''") + "'", null);
After this I got new content in thumbnail icon.
You don't mention what class and inheritance you have; if you are in a class the inherits BaseAdapter, then you can do
notifyDataSetChanged();
after you change the thumbs.

Image disappears in Android Gallery when renamed

I am trying to rename an image in Gallery taken from Camera to give a new name. When I renamed the file the original image disappears showing 2 black squares . Can any one help me in sorting this issue by telling me procedure to rename Gallery Image.
Path is as follows : /mnt/sdcard/DCIM/Camera
I tried to update the Image title through content provider as below:
ContentValues val = new ContentValues();
val.put(Images.Media.TITLE, "ImageTitle23");
getContentResolver().update(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, val, null, null);
The image title is changed in Gallery.
But when I try to query the updated image as below I am getting the name same as previous one :
final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, MediaStore.Images.Media._ID+"="+id, null, imageOrderBy);
if(imageCursor.moveToFirst()){
int idd = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
Can anyone help me in sorting out this issue?
The MediaStore looks for changes in your pictures BUT it will not react to all changes in real time. Each time you do such a modification, you should ask the MediaScanner to scan this file:
MediaScannerConnection.scanFile(
getApplicationContext(),
new String[]{file.getAbsolutePath()},
null,
new OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Log.v("grokkingandroid",
"file " + path + " was scanned seccessfully: " + uri);
}
});
Also, you should check this blog post that gives a lot of useful informations on this usecase.
The Gallery is interacting with the MediaStore content provider. If you change the name of the image file you probably need to update the field in the content provider.
You can read up on it in the MediaStore reference : http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html
Potentially you can just trigger a refresh of the MediaStore. Look at this thread : How can I refresh MediaStore on Android?

Categories

Resources