I am downloading an image from the Internet to an SD card. First check the file exists on the SD card, if not, download the file. if the file is already on the SD card, check the last modified date of the existing file and compare it with the server file. If the date of the server image file is later than the SD card file, then upload the file otherwise when it returns the file.
I have done it. The file is uploaded to the SD card, I have to check it whether it is a recording method for this task or is it another way to do the same task.
So you could help me in this regard. here is my code:
package com.test; import java.io.File; import java.io.FileOutputStream; import java.net.URL; import java.net.URLConnection; import java.util.Date; import java.util.Iterator; import java.util.Map; import java.util.Set; /*import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; */ import android.app.Activity; import android.graphics.Bitmap; public class FileChache { File sdDir; public FileChache(Activity activity) { // Find the directry to save cache image //Gets the Android external storage directory.---> getExternalStorageState() //MEDIA_MOUNTED if the media is present and mounted at its mount point with read/write access. String sdState = android.os.Environment.getExternalStorageState(); if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { sdDir=new File(android.os.Environment.getExternalStorageDirectory(),"sa"); } else sdDir=activity.getCacheDir(); if(!sdDir.exists()) sdDir.mkdirs(); } public File getFile(String url) { //identify images by hashcode String filename= String.valueOf(url.hashCode()); System.out.println("The File name is:"+filename); // Check whether the file is exist in SD Card File f= new File(sdDir, filename); Date fileDate = new Date(f.lastModified()); System.out.println("The file date is:"+fileDate.toGMTString()); // Check whether the file is exist in SD Card if(!f.exists()) { return f; } else { //Check the last update of file File file= new File(filename); Date date= new Date(f.lastModified()); System.out.println("The last Modified of file is:"+date.toGMTString()); /*HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url);*/ try { URL urll = new URL(url); String ss= String.valueOf(urll.hashCode()); URLConnection conn= urll.openConnection(); Map map = conn.getHeaderFields(); Set set = map.entrySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { System.out.println("The Data is :"+iterator.next()); } String header=conn.getHeaderField("Last-Modified"); System.out.println("The header is:"+header); long header1=conn.getIfModifiedSince(); Date aaa= new Date(header1); System.out.println("The getIFLastmodifiedSince is:----"+aaa); Date ServerDate_timeStamp = new Date(header); System.out.println("The headerDate(The serverDate_Timestamp) is:"+ServerDate_timeStamp); long filemodifiedDate= f.lastModified(); System.out.println("The last(Long) modified is:"+filemodifiedDate); Date SD_cardFileModifiedDate = new Date(filemodifiedDate); System.out.println("File file Modified Date Date is:"+ SD_cardFileModifiedDate); if(SD_cardFileModifiedDate.before(ServerDate_timeStamp)) { File ff= new File(sdDir,ss); return ff; } else { return f; } } catch(Exception e) { e.printStackTrace(); } } return null; } public void writeFile(Bitmap bmp, File f) { FileOutputStream out = null; try { out = new FileOutputStream(f); bmp.compress(Bitmap.CompressFormat.PNG, 80, out); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null ) out.close(); } catch(Exception ex) {} } } } /*File f= new File(sdDir,filename); System.out.println("***The last modified time of file is:****"+f.lastModified()); Date d= new Date(f.lastModified()); System.out.println("The lst modified Date is:"+d.toGMTString()); Calendar calendar = Calendar.getInstance(); Date mNew_timeStamp = calendar.getTime(); System.out.println("*****new Time_Stamp:*****"+mNew_timeStamp.toGMTString()); if(f.exists()) { System.out.println("The file is exist"); } else { System.out.println("It does not exist"); }*/ //if(header) // System.out.println(conn.getHeaderFieldKey(j)+":"+ header); /*HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget);*/
source share