androidimagegallerydatecreated

Android need help to sort saving images from web


I have a problem when saving image from web.

I create new folder and save all images to this. The problem is I want this folder order by the last download image on first but When i open gallery, the folder is order by the image date created and the date created in the downloaded image is not the time I download but the first time it created. I've already search on stack over flow and see that java can't modified the date created in image to the time I download it.

Any one has the solution? (Sorry for the bad English)

Thanks you for the comment. I will explain more details.

First, I download image from web to cache directory

HttpURLConnection localHttpURLConnection = (HttpURLConnection) new java.net.URL(urldisplay).openConnection(); 
localHttpURLConnection.setConnectTimeout(30000);
localHttpURLConnection.setReadTimeout(30000);
localHttpURLConnection.setInstanceFollowRedirects(true);
InputStream in = localHttpURLConnection.getInputStream();
File localFile = Constans.fileCache.getCacheFile(urldisplay);
FileOutputStream fos = new FileOutputStream(localFile);
Utils.CopyStream(in, fos); // simple copy by trunks
fos.close();

Second, I copy downloaded image to external storage

File toFile = new File(Environment.getExternalStorageDirectory() + "/folder", "folder_" + System.currentTimeMillis() + ".png");
FileOutputStream fos = new FileOutputStream(toFile);
Utils.CopyStream(new FileInputStream(fromFile), fos);
fos.close();

// Scan image to display when open with gallery otherwise it couldn't see in gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(toFie);
mediaScanIntent.setData(contentUri);
mContext.sendBroadcast(mediaScanIntent);

Lastly, I see that gallery don't sort my image by the time I downloaded. That is the problem i want to fix.


Solution

  • Not sure I understood, but let's try.

    First the issue you mention is more specific to Gallery application than a java code issue. I assume Gallery use EXIF information to order the picture by date they were taken, not oder they are downloaded/copied. Unfortunatly Gallery does not provide any option to sort picture in other oders.

    Maybe you can try to use another explorer that allows you to sort the pictures in another order (maybe ESFileExplore which has more options?)

    Ultimate solution: you can try to change EXIF in your pictures using a java EXIF library to modify picture taken date and this should change the order they appear in the Galery (but very ugly solution...). Some random EXIF libraries after 5 seconds of Google: http://drewnoakes.com/code/exif/ http://www.toanthang.net/modules.php?name=News&new_topic=2&catid=7

    Hope this helps Thierry